如何在Wordpress中获取特定类别的帖子?

时间:2012-10-14 04:32:22

标签: php wordpress wordpress-theming

我想知道在Wordpress中是否有一个函数,我可以在特定的cateogires中获取所有帖子,并根据类别对它们进行排序,这样每个类别都会有一系列属于它的帖子。

我尝试了get_posts功能,但没有运气:

$args      = array(
'numberposts' => 15,
'category' => '161,165,166,1',
);
$postslist = get_posts($args);

2 个答案:

答案 0 :(得分:0)

你应该把WP帖子的ID和functon get_post用于检索作为数组的帖子数据。

<?php
    $my_id = 7;
    $post_id_7 = get_post($my_id, ARRAY_A);
    $title = $post_id_7['post_title'];
?> 

完整参考:http://codex.wordpress.org/Function_Reference/get_post

如果您想按类别ID获取帖子,请使用:

$post_categories = wp_get_post_categories( $post_id );
$cats = array();

foreach($post_categories as $c){
    $cat = get_category( $c );
    $cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
}

或转储完整数据:

   var_dump($cat);

答案 1 :(得分:0)

您应该使用query_postscategory__incategory__and参数 - http://codex.wordpress.org/Function_Reference/query_posts

$args = array( 'category__in' => array(161,165,166,1), 'posts_per_page' => 15 );
query_posts( $args );
while (have_posts()): the_post();
    the_title();
endwhile;