知更鸟 的这段代码实现起来更为方便。 以下代码实际上使用query_posts()函数调取分类目录下的文章,showposts是调取的数量。
<?php
$cats = get_categories();
foreach ( $cats as $cat ) {
query_posts( 'showposts=10&cat=' . $cat->cat_ID );
?>
<h3><?php echo $cat->cat_name; ?></h3>
<ul class="sitemap-list">
<?php while ( have_posts() ) { the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php } wp_reset_query(); ?>
</ul>
<?php } ?>在官方文档中,这样强调:“如果我们不得不用到query_posts(),必须确保每次使用query_posts()后同时执行wp_reset_query();”。这就是为什么在上面的代码中加上了wp_reset_query()的原因。
修改其中的数字10可以设定显示的篇数,可用于在单页面上显示全部分类文章。
如果你对这类的文章感兴趣,建议你好好学习query_posts建议你阅读以下文章
