Wordpress 从子类别获取所有帖子

Wordpress 从子类别获取所有帖子,wordpress,loops,Wordpress,Loops,我需要从以下子类别获取所有帖子: 1-主(id=7) --1.1类别 --1.2类别 --1.3类别 主类别id=7,需要忽略此类别并从子类别中获取所有文章,而不分页。首先获取类别的子类别: $sub_cats = get_term_children( 7, 'category' ); 这将为您提供子类别的ID和数组 然后在wp_query的参数中使用此数组作为税务查询: $args = array( 'post_type' => 'post', 'tax

我需要从以下子类别获取所有帖子:

1-主(id=7)

--1.1类别

--1.2类别

--1.3类别


主类别id=7,需要忽略此类别并从子类别中获取所有文章,而不分页。

首先获取类别的子类别:

$sub_cats = get_term_children( 7, 'category' );
这将为您提供子类别的ID和数组

然后在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 {}
$args=array(
“post_type”=>“post”,
“tax_query”=>数组(
排列(
“分类法”=>“类别”,
“字段”=>“id”,
“术语”=>$sub_猫
),
),
);
$thew_query=newwp_query($args);
if($the\u query->have\u posts()){
while($the\u query->have\u posts()){
$the_query->the_post();
回显“”。获取标题();
}
wp_reset_postdata();
}else{}