如果使用以下代码:
<?php $cats = get_categories(\'child_of=6\');
foreach ($cats as $cat) :
$this_category = get_category($cat);
$args = array(
\'category__in\' => array($cat->term_id)
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) : ?>
<li><a href="<?php echo get_category_link($cat); ?>"><?php echo $cat->name; ?></a>
<?php if ($this_category->category_parent != 0) { ?>
<ul class="children">
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php /*general loop output; for instance: */ ?>
<li>- <a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php } ?>
</li>
<?php else :
echo \'No Posts for \'.$cat->name;
endif;
endforeach;
?>
您将得到如下结果:
子类别1
* - Post title 1
* - Post title 2
子类别2
* - Post title 3
正如您所看到的,每个子类别将只显示该子类别中的帖子。注释I移动
$this_category = get_category($cat);
在foreach内部。