繁体   English   中英

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

[英]How to get posts for specific categories in Wordpress?

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

我试图给我们get_posts函数,但没有运气:

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

您应该将WP po​​st的ID和functon get_post一起放入以将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);

您应该使用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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM