WordPress 非插件相关日志的两种方法
今天博客为日志末尾建立了相关日志链接,试用了wordpress-related-posts插件失败了。于是想到了非插件,在主题文件的single.php插入代码,找了几种代码都有个bug:单个日志的评论会显示到其他相关日志上。其中life stodio博客中提到的代码可用:
在单篇日志中显示相关日志:
在 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>
2010/09/02 10:05:05
这个挺好
2010/08/26 12:31:17
这个挺好。