WordPress教程

代码WordPress实现相关文章展示

阿里云

今天给自己的博客文章详情页添加了相关文章的列表,这样有助于提高用户访问量,降低跳出率。网上也有许多插件实现的(比如:无觅),但我一般都不喜欢用插件,插件的优点是配置简单,但是可能会对网站的速度造成一些小的影响,所以很多人还是比较喜欢用代码实现需要的功能,但是话又说回来了,代码实现也有缺点,就是配置复杂,不懂代码的人完全摸不着头脑或者只能照搬别人的代码,还不如用插件。好了,下面进入正题!

方法一

首先获取文章的所有标签,接着获取这些标签下的 n 篇文章,那么这 n 篇文章就是与该文章相关的文章了。现在可以见到的 WordPress 相关文章插件都是使用的这个方法。下面是实现的代码:

也想出现在这里?联系我们
创客主机
  1. <ul id="tags_related">
  2. <?php
  3. $post_tags = wp_get_post_tags($post->ID);
  4. if ($post_tags) {
  5. foreach ($post_tags as $tag) 
  6. {
  7.     // 获取标签列表
  8.     $tag_list[] .= $tag->term_id;
  9. }
  10. // 随机获取标签列表中的一个标签
  11. $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
  12. // 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表
  13. $args = array(
  14.         'tag__in' => array($post_tag),
  15.         'category__not_in' => array(NULL),      // 不包括的分类ID
  16.         'post__not_in' => array($post->ID),
  17.         'showposts' => 6,               // 显示相关文章数量
  18.         'caller_get_posts' => 1
  19.     );
  20. query_posts($args);
  21. if (have_posts()) : 
  22.     while (have_posts()) : the_post(); update_post_caches($posts); ?>
  23. <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
  24. <?php endwhile; else : ?>
  25.     <li>* 暂无相关文章</li>
  26. <?php endif; wp_reset_query(); } ?>
  27. </ul>

使用说明:”不包括的分类 ID” 指的是相关文章不显示该分类下的文章,将同行的 NULL 改成文章分类的 ID 即可,多个 ID 就用半角逗号隔开。因为这里限制只显示 6 篇相关文章,所以不管给 query_posts() 的参数 tag__in 赋多少个值,都是只显示一个标签下的 6 篇文章,除非第一个标签有 1 篇,第二个标签有 2 篇,第三个有 3 篇。。。。。。所以如果这篇文章有多个标签,那么我们采取的做法是随机获取一个标签的 id,赋值给 tag__in 这个参数,获取该标签下的 6 篇文章。

方法二

本方法是通过获取该文章的分类 id,然后获取该分类下的文章,来达到获取相关文章的目的。

  1. <ul id="cat_related">
  2. <?php
  3. $cats = wp_get_post_categories($post->ID);
  4. if ($cats) {
  5. $cat = get_category( $cats[0] );
  6. $first_cat = $cat->cat_ID;
  7. $args = array(
  8.         'category__in' => array($first_cat),
  9.         'post__not_in' => array($post->ID),
  10.         'showposts' => 6,
  11.         'caller_get_posts' => 1);
  12. query_posts($args);
  13. if (have_posts()) : 
  14. while (have_posts()) : the_post(); update_post_caches($posts); ?>
  15. <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute();
  16.  ?>"><?php the_title(); ?></a></li>
  17. <?php endwhile; else : ?>
  18. <li>* 暂无相关文章</li>
  19. <?php endif; wp_reset_query(); } ?>
  20. </ul>

方法三

相关文章的获取思路是:Tags 标签相关>同分类下文章,也就是说,先获取标签相同的文章,如果还达不到数量,就调用该分类下的文章补足。获取方法貌似最初来自 Willin Kan 大师,然后我参考了倡萌的写法,加上了日期的字段。我先贴一下实现的代码:

  1. <ul>
  2. <?php
  3.   $post_num = 8; // 默认展示8篇文章,可以自行修改~
  4.   $exclude_id = $post->ID;
  5.   $posttags = get_the_tags(); $i = 0;
  6.   if ( $posttags ) {
  7.     $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
  8.     $args = array(
  9.       'post_status' => 'publish',
  10.       'tag__in' => explode(',', $tags),
  11.       'post__not_in' => explode(',', $exclude_id),
  12.       'caller_get_posts' => 1,
  13.       'orderby' => 'comment_date',
  14.       'posts_per_page' => $post_num,
  15.     );
  16.     query_posts($args);
  17.     while( have_posts() ) { the_post(); ?>
  18.       <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a><span class="pub-time"><?php the_time('m月d日') ?></span></li>
  19.     <?php
  20.       $exclude_id .= ',' . $post->ID; $i ++;
  21.     } wp_reset_query();
  22.   }
  23.   if ( $i < $post_num ) {
  24.     $cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
  25.     $args = array(
  26.       'category__in' => explode(',', $cats),
  27.       'post__not_in' => explode(',', $exclude_id),
  28.       'caller_get_posts' => 1,
  29.       'orderby' => 'comment_date',
  30.       'posts_per_page' => $post_num - $i
  31.     );
  32.     query_posts($args);
  33.     while( have_posts() ) { the_post(); ?>
  34.       <li><a rel="bookmark" href="<?php the_permalink(); ?>"  title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a><span class="pub-time"><?php the_time('m月d日') ?></span></li>
  35.     <?php $i++;
  36.     } wp_reset_query();
  37.   }
  38.   if ( $i  == 0 )  echo '<li>没有相关文章!</li>';
  39.   ?>
  40. </ul>

将上面代码贴到详情页里面即可~ DOM 结构你自己根据主题来,css 样式的话自己根据主题修改,我只给个参考:

  1. .read-list ul{margin:0;padding:0}
  2. .read-list ul li{padding:2px 0;margin:0;font-family:Fontawesome;width:100%;list-style: square}
  3. .read-list ul li a{white-space:nowrap;overflow:hidden;text-overflow:ellipsis;display:block;font-size:14px;color:#333;width:85%;float:left}
  4. .read-list ul li span{float:right;color:#999;font-size:12px}


好了,本文到此结束,希望对你有帮助,如果还有什么疑问或者建议,可以多多交流,原创文章,文笔有限,才疏学浅,文中若有不正之处,万望告知。如果觉得文章对你有帮助,请点个赞或者打赏支持一下,谢谢!

代码 WordPress 实现相关文章展示

已有 460 人购买
查看演示升级 VIP立刻购买

收藏
(1)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!