我们在制作wordpress主题的时候经常会为文章模板添加一些相关文章的功能丰富,他们有的时候出现在侧栏,有的时候出现在文章的底部相关文章这块,当然WordPress相关文章的插件也有很多,但是为了这么个小功能去用插件就有点大材小用了,其实我们可以通过代码完成这样的一个小功能!

通过增加WordPress相关文章的功能,能够让访客更多的浏览我们的网站,从而增加PV,友好体验!

下面我们就罗列几个实现WordPress相关文章的几种方法:

方法一:标签相关

首先获取文章的所有标签,接着获取这些标签下的 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.   foreach ($post_tags as $tag) {
  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()) {
  23.       the_post(); update_post_caches($posts); ?>
  24.     <li>* <a href=“<?php the_permalink(); ?>” rel=“bookmark” title=“<?php the_title_attribute(); ?>”><?php the_title(); ?></a></li>
  25. <?php
  26.     }
  27.   }
  28.   else {
  29.     echo ‘<li>* 暂无相关文章</li>’;
  30.   }
  31.   wp_reset_query();
  32. }
  33. else {
  34.   echo ‘<li>* 暂无相关文章</li>’;
  35. }
  36. ?>
  37. </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.   if (have_posts()) {
  14.     while (have_posts()) {
  15.       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
  18.     }
  19.   }
  20.   else {
  21.     echo ‘<li>* 暂无相关文章</li>’;
  22.   }
  23.   wp_reset_query();
  24. }
  25. else {
  26.   echo ‘<li>* 暂无相关文章</li>’;
  27. }
  28. ?>
  29. </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.         $tag_list .= $tag->term_id.’,’;
  10.     }
  11.     $tag_list = substr($tag_list, 0, strlen($tag_list)-1);
  12.     $related_posts = $wpdb->get_results(
  13.         SELECT DISTINCT ID, post_title
  14.         FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
  15.         WHERE {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
  16.         AND ID = object_id
  17.         AND taxonomy = ‘post_tag’
  18.         AND post_status = ‘publish’
  19.         AND post_type = ‘post’
  20.         AND term_id IN (” . $tag_list . “)
  21.         AND ID != ‘” . $post->ID . “‘
  22.         ORDER BY RAND()
  23.         LIMIT 6″);
  24.         // 以上代码中的 6 为限制只获取6篇相关文章
  25.         // 通过修改数字 6,可修改你想要的文章数量
  26.     if ( $related_posts ) {
  27.         foreach ($related_posts as $related_post) {
  28. ?>
  29.     <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>
  30. <?php   }
  31.     }
  32.     else {
  33.       echo ‘<li>暂无相关文章</li>’;
  34.     }
  35. }
  36. else {
  37.   echo ‘<li>暂无相关文章</li>’;
  38. }
  39. ?>
  40. </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.   $related = $wpdb->get_results(
  7.   SELECT post_title, ID
  8.   FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
  9.   WHERE {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id
  10.   AND {$wpdb->prefix}term_taxonomy.taxonomy = ‘category’
  11.   AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
  12.   AND {$wpdb->prefix}posts.post_status = ‘publish’
  13.   AND {$wpdb->prefix}posts.post_type = ‘post’
  14.   AND {$wpdb->prefix}term_taxonomy.term_id = ‘” . $cats[0] . “‘
  15.   AND {$wpdb->prefix}posts.ID != ‘” . $post->ID . “‘
  16.   ORDER BY RAND( )
  17.   LIMIT 6″);
  18.   if ( $related ) {
  19.       foreach ($related as $related_post) {
  20. ?>
  21.     <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>
  22. <?php
  23.     }
  24.   }
  25.   else {
  26.     echo ‘<li>* 暂无相关文章</li>’;
  27.   }
  28. }
  29. else {
  30.   echo ‘<li>* 暂无相关文章</li>’;
  31. }
  32. ?>
  33. </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.   if (have_posts()) {
  14.     while (have_posts()) {
  15.       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
  18.     }
  19.   }
  20.   else {
  21.     echo ‘<li>* 暂无相关文章</li>’;
  22.   }
  23.   wp_reset_query();
  24. ?>
  25. </ul>

时间效率对比

我们将用之前的一个php代码对以上各个相关文章代码执行时间进行测算,以便对以上各个的方法进行效率,给你的选择提供参考。以下是在同一篇文章中获取6篇相关文章,以上各方法最终测算的时间如下:

方法一:0.18067908287048 秒

方法二:0.057158946990967 秒

方法三:0.037126064300537 秒

方法四:0.045628070831299 秒

方法五:0.023991823196411 秒


新软师兄 » wordpress代码实现相关文章的几种方法
50T免费网盘资源大集合【持续更中~~~~】:点击查看

dase kand pornhan.mobi xvideo desi gay pcso 2pm result today pinoytvfriends.com where i can watch bad romeo كلام فى النيك wfporn.com قصص محارم حديثة busporn porngugu.mobi indian sexx vedios sex ka video noticieroporno.com himachal pradesh sex com
nero hentai hentaitgp.com ламия хентай www.mom xxx.com alohaporn.me sahara knite mature fucking tubepatrolporn.com bhabi sex indian girl sex gotporn.mobi xnxx family strocks ang probinsyano july 20 2022 full episode youtube pilipinoteleserye.com ano ang pambansang sasakyan ng pilipinas
احلي سكس محارم pornxporn.org نيك فلاحى multi.xnxx alohaporn.net telugu sex chart سكس قصيرات arabysexy.org نيك نقاب www assames sex com umora.info desi sexy bhabi 8teenx bukaporn.com india hot sex videos