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

      Discuz! X1 首页显示论坛的修改方法

      解决了Discuz! X1 UC后台应用通讯…

    2. 没有图片

      2011.01.05

      WordPress 模版参数(一)

      <?php the_author_ni…

    3. 没有图片

      2011.02.18

      ECMall商城系统去掉首页标题中的“商城首页”教程

      很多朋友用ECMall商城系统做多用户商城,…

    4. 没有图片

      2012.07.02

      [父母必看]一位年轻妈妈的亲身经历

      平时我博客一般不发这类的内容的日志的,今天看…

    5. 没有图片

      2010.05.08

      国外优秀UI网址集锦

      丹麦Icon设计师的主页 http://jo…

    6. 2016.12.14

      一文告诉你究竟适合创业还是打工

      创业绝对不是逃避现状的出路 首先,创业与上班…

    评论

    还没有评论。