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

      手机浏览器 UC浏览器7.1正式版 For iPhone

      随着3G网络的流行,手机上网已经成为大家很正…

    2. 没有图片

      2011.01.14

      给WordPress的下载链接自动添加图片

      有时想让你的下载链接直接变成漂亮的图片按钮,…

    3. 没有图片

      2010.05.15

      人这一辈子 养生版

      人这一辈子,怎么都是过,与其皱眉头,不如偷着…

    4. 没有图片

      2010.11.02

      PSD to HTML工作标准

      一.每个psd源图都有三种配色方案,要求按照…

    5. 没有图片

      2010.05.11

      初次写博技术文章,遇上不速之客

      今天费了很大的劲翻译了Arne的WordPr…

    6. 没有图片

      2011.01.11

      每日学习之:倍增你的学习力

      学习的动力、学习的毅力以及学习的能力,才是你…

    评论

    还没有评论。