实现 WordPress 的常用功能可以不用插件自然是不用。比如 WordPress 相关日志功能,使用已经为童鞋们介绍过的插件 WordPress Related Posts可以实现。下面分享不用此插件即可实现相关日志功能,添加代码到 single.php 模板文件上面即可。完成后就可以删除相关日志插件!

<?php
$backup = $post;
$tags = wp_get_post_tags($post->ID);
$tagIDs = array();
if ($tags) {
echo ‘<h4>相关日志</h4>’;
echo ‘<ul>’;
$tagcount = count($tags);
for ($i = 0; $i < $tagcount; $i++) {
$tagIDs[$i] = $tags[$i]->term_id;
}
$args=array(
‘tag__in’ => $tagIDs,
‘post__not_in’ => array($post->ID),
‘showposts’=>4,<!– 显示相关日志篇数 –>
‘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(); ?>”><?php the_title();  ?></a></li>
<?php endwhile;
echo ‘</ul>’;
} else { ?>
<ul>
<?php
query_posts(array(‘orderby’ => ‘rand’, ‘showposts’ => 8));<!–  显示随机日志篇数 –>
if (have_posts()) :
while (have_posts()) : the_post();?>
<li><a href=”<?php the_permalink() ?>” rel=”bookmark”  title=”Permanent Link to <?php the_title(); ?>”><?php  the_title(); ?></a></li>
<?php endwhile;endif; ?>
</ul>
<?php }
}
$post = $backup;
wp_reset_query();
?>

注:使用时,代码中的注释内容需要删除,即:<!– 显示相关日志篇数 –>与<!– 显示随机日志篇数 –>