Loading...

继续折腾-无插件实现 wordpress 相关文章功能

之前不知道在哪里看到的关于 wordpress 相关文章的插件介绍,最后选择了yet another related posts plugin 这款相关文章插件。可能是自己设置得不够好,所以在很多文章里看到的相关文章都特别地少,偶尔气愤一下,同个关键词,也相关不到。既然追求速度,插件能少一个是一个。想办法无插件实现 wordpress 相关文章功能,在网上找到万戈老大的文章《WordPress 非插件相关日志的两种方法》,容易简单,但功能基本能满足博客基本的要求,暂时不考虑Feed的输出,所以决定采用其中的第2种。开始折腾吧。
仅在单篇日志中显示相关日志。
在 WordPress 主题文件 single.php 中需要的位置插入以下代码即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<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>

并在这段代码包加< div id=”related_posts”> < /div>,直接调inove主题下载的相关文章样式,当然可以根据自己的需要修改。

至于,单篇日志和 feed 中都可以生成相关日志,有需要的童鞋可以去万戈那里看看。

无觅相关文章插件

  1. August 29th, 2010 at 17:16 | #1

    哈哈,不知道站长大几了啊

    [Reply]

    alexin Reply:

    @sky, 9月份开始,研一啊。 :cool:

    [Reply]

  2. March 2nd, 2011 at 17:55 | #2

    太棒了,要的就是这个!爱死你了,亲一个…… :grin:

    [Reply]

  3. May 1st, 2011 at 15:14 | #3

    不用插件最好,安装很多插件会把网站拖跨

    [Reply]

Comment pages
1 2 271
  1. No trackbacks yet.
疑问 冷笑 悲伤 坏蛋 感叹 微笑 脸红 大笑 吃惊 惊讶 困惑 酷 大声笑 恼火 古怪 转眼睛 给眼色 好主意 箭头 一般 哭了 绿人