@Scott B,
看起来他们正在使用基于类别的相关帖子查询。当显示单个项目(帖子)时,查询将查找类别id,然后显示来自同一类别的帖子。
Add The following code to your sidebar or even to the bottom of single.php depending on where you want to display the "Related Posts"
<!--Begin Related Posts-->
<?php
if ( is_single() ) :
global $post;
$categories = get_the_category();
foreach ($categories as $category) :
$posts = get_posts(\'numberposts=4&exclude=\' . $GLOBALS[\'current_id\'] . \'&category=\'. $category->term_id);
//To change the number of posts, edit the \'numberposts\' parameter above
if(count($posts) > 1) {
?>
<div class="widget" id="more-category">
<h3 class="widgettitle"><?php _e(\'More in\',\'\'); ?> ‘<?php echo $category->name; ?>’</h3>
<ul>
<?php foreach($posts as $post) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php } ?>
<?php endforeach; ?>
<?php endif; ?>
<!--/related posts-->
这样做的目的是首先获取当前显示的帖子的类别,从查询中省略当前项目,然后进行检查以确保该类别中有1篇以上的帖子。
如果有,那么它将输出“更多在您的类别名称”后面的永久链接到帖子。如果要显示特色图像,可以将最后一部分更改为如下所示:
<div class="widget" id="more-category">
<h3 class="widgettitle"><?php _e(\'More in\',\'\'); ?> ‘<?php echo $category->name; ?>’</h3>
<ul>
<?php foreach($posts as $post) : ?>
<li><?php the_post_thumbnail(); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
</div>