WordPress 不用插件实现相关日志的办法

WordPress 的可以实现相关日志功能的插件确实有很多,比如说 Related Posts、Simple Tags、Yet Another Related Posts等插件,功能都很强大,可以简单实现相关日志功能。而本文为大家介绍的是不用插件实现相关日志的方法。(本文引自万戈博客,适当改了一下)

方法一:单篇日志和 feed 中都可以生成相关日志。

此方法参考自荒野无灯的,可以设置的参数很多,也相当之强大。不过我还是嫌代码太多,所以根据自己的需要再一次精简压缩了一下,总结如下:

把以下代码复制到 WordPress 的主题文件 functions.php 中:

function wp_get_related_posts()
{
global $wpdb, $post,$table_prefix;
$limit = 10; //显示几条相关文章
if(!$post->ID){return;}
$now = current_time('mysql', 1);
$tags = wp_get_post_tags($post->ID);
$taglist = "'" . $tags[0]->term_id. "'";
$tagcount = count($tags);
if ($tagcount > 1) {
    for ($i = 1; $i < $tagcount; $i++) {
        $taglist = $taglist . ", '" . $tags[$i]->term_id . "'";
    }
}
    $limitclause = "LIMIT $limit";
$q = "SELECT p.ID, p.post_title, p.post_date,  p.comment_count, count(t_r.object_id) as cnt FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships t_r, $wpdb->posts p WHERE t_t.taxonomy ='post_tag' AND t_t.term_taxonomy_id = t_r.term_taxonomy_id AND t_r.object_id  = p.ID AND (t_t.term_id IN ($taglist)) AND p.ID != $post->ID AND p.post_status = 'publish' AND p.post_date_gmt < '$now' GROUP BY t_r.object_id ORDER BY cnt DESC, p.post_date_gmt DESC $limitclause;";
$related_posts = $wpdb->get_results($q);
$output = "";
if (!$related_posts)
{
    $output  .= '
  • 无相关日志
  • '; } foreach ($related_posts as $related_post ) { $dateformat = get_option('date_format'); $output .= '
  • '; $output .= ''.wptexturize($related_post->post_title).' ('. $related_post->comment_count .')'; $output .= '
  • '; } $output = '

    相关日志

      ' . $output . '
    '; return $output; } function wp_related_posts_attach($content) { if (is_single()||is_feed()) { $output = wp_get_related_posts(); $content = $content . $output; } return $content; } add_filter('the_content', 'wp_related_posts_attach',100);

    方法二:仅在单篇日志中显示相关日志。

      昨天在群里做了个小调查,发现会在阅读器里点击相关文章的童鞋并不多,所以比我更加有代码洁癖的童鞋可以索性只在单篇日志中插件相关文章,那样代码会精简不少。

      在 WordPress 主题文件 single.php 中需要的位置插入以下代码即可:

    相关日志

      < ?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(); ?>
    • < ?php the_title();?> < ?php comments_number(' ','(1)','(%)'); ?>
    • < ?php endwhile; } } wp_reset_query(); ?>

    至于这两个方法实现的文章的相关度,大家可以放心,因为都是以标签为数组获取的相关文章,相关度还是很高的,和插件实现的一样。

    思章老师

    认准了方向,就要勇敢地走下去,十年磨一剑,我相信,只要坚持,一切都有可能。

    相关日志

    1. 没有图片

      2010.10.13

      一篇与房子有关的话题:房子野话

      以前,有个地主有很多地,找了很多长工干活,地…

    2. 没有图片

      2010.06.13

      WordPress 3.0 RC3发布

      今日WordPress3.0 RC3发布,这…

    3. 没有图片

      2010.05.26

      再挖WordPress3.0版新亮点

      WordPress 3.0 Beta 2正式…

    4. 没有图片

      2010.06.16

      10个Web2.0配色方案网站

      这10个网站有的可以在线配色,有的是提供一些…

    5. 没有图片

      2010.05.14

      用一个WordPress 建多个不同的博客

      用WordPress建博客的肱都知道,一个W…

    6. 没有图片

      2010.06.21

      Custom Post Type UI创建WordPress自定义内容类型

      自从Wordpress 2.9以后的版本,W…

    评论

    还没有评论。