WP_Query类别树

时间:2017-08-18 11:18:42

标签: wordpress

我有一个显示特定类别图像的循环。

$args = array(
'posts_per_page' => 10,   
'post_status' => 'inherit', 
'post_type'=> 'attachment',
'cat' => 777
); 

$wp_query = new WP_Query($args);

我希望在该类别的帖子中获得属于该类别后代的帖子。谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用get_term_children()来首先获取该类别的后代。然后,您可以在category__in键下的查询参数中传递术语ID列表:

$category_id = 777;
$term_ids = get_term_children($category_id, 'category');

$args = array(
    'posts_per_page' => 10,   
    'post_status' => 'inherit', 
    'post_type'=> 'attachment',
    'category__in' => array($category_id) + $term_ids
);

$wp_query = new WP_Query($args);