今天给大家介绍一下在wordpress 文章末尾加上相关文章,因为之前的加了之后,个别朋友提出,有些主题调用标签会出错,所以修改了另一个版本的!亲测可用!
先看案例
具体案例可以 在文章里面看到
怎么实现的呢?下面是代码,复制代码到 文章函数 (single.php) 对应的位置里面 即可
因为各个主题不一样,这里以知更鸟4.4 为例
在主题编辑里面找到 文章底部对应的位置
<!-- 相关文章开始 -->
<ul class="spostinfo">
<h3>80%的人都看过的文章</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>';
?>
<br/><li><strong><?php _e( '版权声明:', 'begin' ); ?></strong>本文版权归<a href="http://bushen.me">补肾么</a>和原作者所有,未经许可不得转载。文章部分来源于网络仅代表作者看法,如有不同观点,欢迎进行交流。除非注明,文章均由 <a href="http://bushen.me">补肾么</a> 整理发布,欢迎转载,转载请带版权。</li><br/>
<li class="reprinted"><strong><?php _e( '来源: ', 'begin' ); ?></strong> 补肾么(http://bushen.me/),分享补肾壮阳知识,解答补肾壮阳问题,分享壮阳补肾秘方。<p>本文链接:<a href="<?php echo Bing_current_url();?>" rel="bookmark" title="本文固定链接 <?php echo Bing_current_url();?>"><?php echo Bing_current_url();?></a></p></li>
</ul>
<!-- 相关文章结束 -->
具体可参考:http://bushen.me/zhuangyangff/133.html