不用插件实现wordpress相关文章功能

给博客加上相关文章功能可以有效提高PV,也方便读者快速阅读。实现它很简单。可以用插件,推荐Yet Another Related Posts Plugin (YARPP),优点:

  • 可以设置显示相关文章的相关度门槛(高于此值才显示为相关文章);
  • 改进了查询相关度的算法,文章的分类和标签是计算相关度的参数;
  • 可以在 RSS feed 中显示相关文章;
  • 可以在文章后面自动插入相关文章。
  • 其核心功能无非是要实现相关文章匹配,一样可以用代码实现,而且是根绝文章标签判断,加载更快,也能自定义CSS样式。

    <ol><?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'=>6,
    'caller_get_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?> <?php comments_number(' ','(1)','(%)'); ?></a> </li>
    <?php
    endwhile;
    }
    }
    ?>
    </ol>

    效果见本站各篇文章底部,如果有相关的标签即显示,没有的话为空。

    可更改showposts的值,控制显示的数量,请自行测试。

    不用插件实现wordpress相关文章功能》上有 3 条评论

    发表评论

    电子邮件地址不会被公开。 必填项已用 * 标注

    *

    您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">