WordPress相关日志函数

不用插件实现一些常用功能是上上之选,之前网上盛传的不使用插件就能实现的五个常用功能一文中的相关日志函数,不是很完善,加到单篇日志后,下面的评论会变成最后一篇相关日志的评论,大概是loop出了问题。而这段代码不会出现这个弊病,并且在没有相关日志的情况下,会显示随机日志。这段代码是从一款主题中Copy出来的,原函数在没有相关日志时,显示最新文章,修改为显示随机日志。将这段代码加到single.php模板文件<?php comments_template(); ?>上面即可,可以删除相关日志插件了!

<?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();
?>

    © 版权声明
    THE END
    喜欢就支持一下吧
    点赞0分享
    评论 抢沙发

    请登录后发表评论

      暂无评论内容