WordPress通过分类显示相关文章列表
将下面的代码放到single.php文件里面的<?php comments_template( ”, true ); ?>上面,位置和样式请根据自己想法进行调整。
<div id="recent-post"> <h2>相关文章列表</h2> <ul> <?php $catid = get_the_category(); //通过这个函数可以获得该文章下的分类信息 $newcat_id = $catid[0]->cat_ID; //获取得到该文章下的分类ID $recent = new WP_Query("showposts=5&orderby=ID&cat=".$newcat_id); while($recent->have_posts()) : $recent->the_post(); ?> <li><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endwhile; wp_reset_query(); ?> </ul> </div> //showposts=5,5表示显示5条相关文章,请根据自己需要进行设置。
效果如本文章尾部。