很多主题的文章页面尾部会显示一些与当前文章相关的内容,这样做一方面扩展了页面,让页面看起来更为丰富,同时也给予了站内其他文章更多的展示机会。要达成这样的效果,并不需要依靠插件,只需要在合适的位置添加一段代码:
参考代码
<h3>相关文章</h3>
<ul class="related_posts">
<?php
$post_num = 8;
$exclude_id = $post->ID;
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) {
$tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
$args = array(
'post_status' => 'publish',
'tag__in' => explode(',', $tags),
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num,
);
query_posts($args);
while( have_posts() ) { the_post(); ?>
<li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php
$exclude_id .= ',' . $post->ID; $i ++;
} wp_reset_query();
}
if ( $i < $post_num ) {
$cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
$args = array(
'category__in' => explode(',', $cats),
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num - $i
);
query_posts($args);
while( have_posts() ) { the_post(); ?>
<li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php $i++;
} wp_reset_query();
}
if ( $i == 0 ) echo '<li>没有相关文章!</li>';
?>
</ul>
以上代码会根据文章标签去生成相关的文章,其中post_num定义的所需要展示的文章数量,如果根据标签所搜寻到的文章数量不足八篇,代码会尝试扩大搜索范围从当前文章所属目录中补足,循环结束最终生成一个h3标题与一个无序列表。
修改代码
因为自己的博客每篇文章只设置了一个标签作为小标题使用,所以不准备利用循环来提取标签,常规的编程语言中可以直接给数组加上序号来提取第一个元素,就象这样“变量[0]”,但php中非索引类的数组似乎不允许这样使用,提取数组第一个元素的内容可以使用函数“current()”,同时因为觉得同目录的文章相关性没有同标签的文章来的强,所以取消了从目录提取补足文章数量的这一段,因为缩小了搜索范围,很容易就出现没有相关文章的情况,干脆把整个模块放进了判断内,没有文章的时候不显示整个模块,所以这段代码里也没有了“没有相关文章”的提示:
<?php
$post_num = 8;
$exclude_id = $post->ID;
$posttags = get_the_tags();
if($posttags){
$args = array(
'post_status' => 'publish',
'tag__in' => current($posttags)->term_id,
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num,);
query_posts($args);
if(have_posts()){
echo '<h3 class="related-title">相关文章</h3>';
echo '<ul class="related-posts">';
while( have_posts() ) { the_post(); ?>
<li><a rel="bookmark" class="related-post-title" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php $exclude_id.=','.$post->ID;}
echo '</ul>';}
wp_reset_query();
}?>
添加元素
代码中我们只输出了相关文章的标题,事实上这段代码的“while(have_posts()){}”中的逻辑跟主循环是一致的,我们可以跟首页文章列表一样往里面添加一些其他的元素,比如文章摘要,输出文章摘要的语句如下:
<?php the_excerpt(); ?>
修改后的代码如下:
<?php
$post_num = 8;
$exclude_id = $post->ID;
$posttags = get_the_tags();
if($posttags){
$args = array(
'post_status' => 'publish',
'tag__in' => current($posttags)->term_id,
'post__not_in' => explode(',', $exclude_id),
'caller_get_posts' => 1,
'orderby' => 'comment_date',
'posts_per_page' => $post_num,);
query_posts($args);
if(have_posts()){
echo '<h3 class="related-title">相关文章</h3>';
echo '<ul class="related-posts">';
while( have_posts() ) { the_post(); ?>
<li>
<a rel="bookmark" class="related-post-title" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
</li>
<?php $exclude_id.=','.$post->ID;}
echo '</ul>';}
wp_reset_query();
}?>
以上代码会根据标签提取最多8篇相关文章,输出文章的标题与摘要,如果该标签下没有相关文章,则不生成相关文章模块。
WP_Query的实现方式
因为WP_Query是更为推荐的实现方式,这里也附上了WP_Query的实现方案,供参考:
<?php $categories = get_the_category(get_the_ID()); ?>
<?php if($categories): ?>
<?php $args = array(
'category__in' => array($categories[0]->term_id),
'post__not_in' => array(get_the_ID()),
'posts_per_page' => 4,
'ignore_sticky_posts' => 1,
); ?>
<?php $postlists_query = new WP_Query($args); ?>
<?php if ($postlists_query->have_posts()) :?>
<?php $term = get_term_by('id', $categories[0]->term_id, 'category'); ?>
<div class="content-module">
<h4 class="content-module-title">相关阅读
<a class="content-module-more" href="<?php echo get_term_link($term); ?>"><span class="content-module-more-i"></span></a>
</h4>
<div class="content-postlist">
<?php while ($postlists_query->have_posts()) : $postlists_query->the_post(); ?>
<article class="content-postlist-item">
<div class="content-postlist-img">
<?php if (has_post_thumbnail()): ?>
<?php the_post_thumbnail(); ?>
<?php else: ?>
<img src="<?php echo TBS_IMAGE_URI.'default_thumb.jpg'; ?>" alt="<?php echo get_the_title(); ?>">
<?php endif; ?>
</div>
<div class="content-postlist-info">
<h3 class="content-postlist-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<p class="content-postlist-excerpt"><?php echo get_the_excerpt(); ?></p>
<?php $tags = get_the_tags(); ?>
<?php if (!empty($tags)): ?>
<div class="content-postlist-meta">
<span class="content-postlist-tags">
<?php foreach ($tags as $tag): ?>
<a href="<?php echo get_tag_link($tag->term_id); ?>">
<i class="fa fa-fw fa-tag" aria-hidden="true" style=""></i><?php echo $tag->name; ?>
</a>
<?php endforeach; ?>
</span>
</div>
<?php endif; ?>
</div>
</article>
<?php endwhile; ?>
</div>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endif; ?>