2017-04-09 107 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 {}