Wordpress无插件实现相关日志的代码

| | Comments (0)



自己用过的代码贴出来,有时排在搜索引擎前面的不一定可以用,可能是时间过长的关系,有些可能失效了,但是自己又不会,只能找现成了,终于找了个可以正常工作的,加入了自己的想法,不错可以拉。

能不用插件实现的功能,坚决支持,如果你的博客内容不错,但是用户停留的时间很短,如果加入相关日志功能,可以提供博客日志的粘性,让用户更多的阅读你的文章。网上终于找到一段代码测试成功,:)将这段代码加到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();
?>



原作者网址:http://www.3anshi.com/wordpress-noplugins-rizhi.html

Leave a comment

Archives

Ads by google

Pages

Powered by Movable Type 4.24-en

About this Entry

This page contains a single entry by 单点日志 published on June 28, 2011 12:55 AM.

WordPress 模板首页和目录页摘要、全文输出的修改方法 was the previous entry in this blog.

百度搜索引擎site功能已阉割,使用百度统计这是必须的! is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.