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.05.19

      Comsenz 核心产品 Discuz! X1正式版发布

      Discuz! X1 Beta出来不久,今天…

    2. 没有图片

      2012.12.28

      美国女生给我的六个惊诧(转)

      此文章转自QQ群里关心中国未来、中国教育的网…

    3. 没有图片

      2010.11.24

      IT下载站,我操你妈!

      菠萝今晚加班回家晚,回来之后就想第一时间装上…

    4. 没有图片

      2011.10.06

      乔布斯,一路走好

      今天上线,看到的到处都是苹果总裁乔布斯去世的…

    5. 没有图片

      2011.01.22

      支持WordPress多站点的Sitemap插件:XML Sitemap Feed

      大家的WordPress博客很多都在使用的G…

    6. 没有图片

    评论

    还没有评论。