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. 没有图片

      2011.01.07

      10款免费的WordPress 3.0主题

      1、Stargaze 2. PhotoBlo…

    2. 没有图片

      2010.06.05

      Google Logo纪念丹尼斯·伽伯(Dennis Gabor)诞辰

      6月5日是英国籍匈牙利裔物理学家是丹尼斯·伽…

    3. 没有图片

      2010.09.13

      教育与政治独立,中国教育才可能有救

      教育事业应该完全交给教育家,保有独立的资格,…

    4. 没有图片

      2010.05.20

      CSS经典布局16例演示+下载

      近来蓦然也是开始学习CSS样式表了,这里转发…

    5. 没有图片

      2011.01.13

      开源免费的Flv播放器

      从飞晏博客那看到一款Flv播放器的介绍,转到…

    6. 没有图片

      2010.05.31

      WordPress升级详细教程(各版本通用)

      升级WordPress已经是我们老生常谈的话…

    评论

    还没有评论。