您可以这样做:
1-获取属于所需术语的所有子术语
$args = array(
\'taxonomy\' => \'product_cat\',
\'child_of\' => 7, put here the id of parent term, which chldren you want to get
\'hide_empty\' => false,
);
$child_terms = get_terms( $args );
2-循环浏览这些术语并打印出这些名称和帖子
if(!empty($child_terms)){
foreach($child_terms as $term){
$args = array(
\'posts_per_page\' => -1,
\'post_type\' => \'product\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'term_id\',
\'terms\' => $term->term_id,
)
),
\'post_status\' => \'publish\',
);
$posts_array = get_posts( $args );
echo $term->name; // subterm name
foreach ($posts_array as $post) {
setup_postdata( $post );
the_title(); //subterm post
}
}
}