2017-04-09 109 views
0

我需要得到子类别中的帖子是这样的:获得儿童类的所有帖子

1 - 主(ID = 7)

--1.1类别

--1.2类别

--1.3类别

主类别的ID = 7,需要忽略此类别,并从没有分页的子类别中获取所有帖子。

回答

1

首先得到该类别的足月儿的:

$sub_cats = get_term_children(7, 'category'); 

这会给你和数组,如果子类的IDS

然后在wp_query的参数使用数组作为税务查询:

$args = array(
     'post_type' => 'post', 
     'tax_query' => array(
      array(
      'taxonomy' => 'category', 
      'field' => 'id', 
      'terms' => $sub_cats 
     ), 
    ), 
); 


$the_query = new WP_Query($args); 

if ($the_query->have_posts()) { 
    while ($the_query->have_posts()) { 
    $the_query->the_post(); 

     echo '<p>' . get_the_title() . '</p>'; 
} 
wp_reset_postdata(); 
} else {}