您当前的位置: 首页 > CMS教程 > WordPress教程 > WordPress代码实现相关文章的几种方法

WordPress代码实现相关文章的几种方法

作者:xiaoxiao 来源:未知 发布时间: 2013-10-18 10:16 点击:
WordPress很多插件可以实现相关文章的功能,插件的优点是配置简单,但是可能会对网站的速度造成一些小的影响,所以很多人还是比较喜欢用 代码实现需要的功能,但是话又说回来了,代码实现也有缺点,就是配置复杂,不懂代码的人完全摸不着头脑或者只能照搬别人的代码,

WordPress代码实现相关文章的几种方法

      WordPress很多插件可以实现相关文章的功能,插件的优点是配置简单,但是可能会对网站的速度造成一些小的影响,所以很多人还是比较喜欢用 代码实现需要的功能,但是话又说回来了,代码实现也有缺点,就是配置复杂,不懂代码的人完全摸不着头脑或者只能照搬别人的代码,还不如用插件。

      这里我整理编写了几种用代码实现相关文章的方法,这其中会详细标明各部分代码的作用,以及如何自定义你想要的功能,希望对大家有所帮助,有什么问题可以给本文发表评论,我会及时给你回复。开始之前,说明一点,以下所有方法输出的HTML代码格式都是以下形式,你可以根据需要进行修改:

  1. <ul id="xxx"> <li><a title="文章标题1" rel="bookmark" href="文章链接1">文章标题1</a></li>
  2.  <li><a title="文章标题2" rel="bookmark" href="文章链接2">文章标题2</a></li> ...... </ul> 

      方法一:标签相关

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

 

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

      方法三:标签相关,SQL获取

      获取相关文章的原理与方法一相似,不过在获取文章的时候是以SQL语句来直接读取数据库,从而随机获取6篇相关文章记录,而不是WordPress的函数query_posts().

 

  1. <ul id="tags_related"> 
  2. <?php 
  3. global $post, $wpdb;  
  4. $post_tags = wp_get_post_tags($post->ID);  
  5. if ($post_tags) {  
  6. $tag_list = '';  
  7. foreach ($post_tags as $tag)  
  8. {  
  9. // 获取标签列表  
  10. $tag_list .= $tag->term_id.',';  
  11. }  
  12. $tag_list = substr($tag_list, 0, strlen($tag_list)-1);  
  13.  
  14. $related_posts = $wpdb->get_results("  
  15. SELECT DISTINCT ID, post_title  
  16. FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy  
  17. WHERE {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id  
  18. AND ID = object_id 
  19. AND taxonomy = 'post_tag' 
  20. AND post_status = 'publish' 
  21. AND post_type = 'post' 
  22. AND term_id IN (" . $tag_list . ")  
  23. AND ID != '" . $post->ID . "'  
  24. ORDER BY RAND()  
  25. LIMIT 6");  
  26. // 以上代码中的 6 为限制只获取6篇相关文章  
  27. // 通过修改数字 6,可修改你想要的文章数量  
  28.  
  29. if ( $related_posts ) {  
  30. foreach ($related_posts as $related_post) {  
  31. ?> 
  32. <li><a href="<?php echo get_permalink($related_post->ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li> 
  33. <?php  } } else { ?> 
  34. <li>暂无相关文章</li> 
  35. <?php } } ?> 
  36. </ul> 

      方法四:分类相关,SQL获取

      获取相关文章的原理与方法二相似,不过在获取文章的时候是以SQL语句来直接读取数据库,从而随机获取6篇相关文章记录,而不是WordPress的函数query_posts().

 

  1. <ul id="cat_related"> 
  2. <?php 
  3. global $post, $wpdb;  
  4. $cats = wp_get_post_categories($post->ID);  
  5. if ($cats) {  
  6.  
  7. $related = $wpdb->get_results("  
  8. SELECT post_title, ID  
  9. FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy  
  10. WHERE {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id  
  11. AND {$wpdb->prefix}term_taxonomy.taxonomy = 'category' 
  12. AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id  
  13. AND {$wpdb->prefix}posts.post_status = 'publish' 
  14. AND {$wpdb->prefix}posts.post_type = 'post' 
  15. AND {$wpdb->prefix}term_taxonomy.term_id = '" . $cats[0] . "' 
  16. AND {$wpdb->prefix}posts.ID != '" . $post->ID . "'  
  17. ORDER BY RAND( )  
  18. LIMIT 6");  
  19.  
  20. if ( $related ) {  
  21. foreach ($related as $related_post) {  
  22. ?> 
  23. <li><a href="<?php echo get_permalink($related_post->ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li> 
  24. <?php  } } else { ?> 
  25. <li>* 暂无相关文章</li> 
  26. <?php } }?> 
  27. </ul> 

      方法五:作者相关

      该方法是获取该文章作者的其他文章来充当相关文章,代码如下:

 

  1. <ul id="author_related"> 
  2. <?php 
  3. global $post;  
  4. $post_author = get_the_author_meta( 'user_login' );  
  5. $args = array(  
  6. 'author_name' => $post_author,  
  7. 'post__not_in' => array($post->ID),  
  8. 'showposts' => 6,               // 显示相关文章数量  
  9. 'orderby' => date,          // 按时间排序  
  10. 'caller_get_posts' => 1  
  11. );  
  12. query_posts($args);  
  13.  
  14. if (have_posts()) :  
  15. while (have_posts()) : the_post(); update_post_caches($posts); ?> 
  16. <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> 
  17. <?php endwhile; else : ?> 
  18. <li>* 暂无相关文章</li> 
  19. <?php endif; wp_reset_query();  ?> 
  20. </ul> 

 

分享到:
本文"WordPress代码实现相关文章的几种方法"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(1)
100%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4