免插件为wordpress添加相关日志功能

以前记得有一个插件可以给文章尾部添加相关文章的,不过都说插件多了不利于博客速度,所以这次决定手动改代码,方法来至万戈

万戈介绍了两种方法,一种是给文章和feed都添加相关文章,另一种就是只给文章添加相关日志,我采取的第二种,方法和代码如下:

在 WordPress 主题文件 single.php 中需要的位置插入以下代码即可:

<h3>相关日志</h3>
<ul>
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
‘tag__in’ => array($first_tag),
‘post__not_in’ => array($post->ID),
‘showposts’=>10,
‘caller_get_posts’=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>”><?php the_title();?> <?php comments_number(‘ ‘,’(1)’,'(%)’); ?></a></li>
<?php
endwhile;
}
}
wp_reset_query();
?>
</ul>
最终效果参加本博客文章底部。