WordPress无插件查询tags和categories实现相关文章

为了增加阅读量,方便读者找到需要的东西,我们常常在文章的底部添加相关文章的功能,我们可以从两个角度考虑,读者可能感兴趣的文章,以及相关文章,前者我们调取相同 tags下的文章随机生成,而相关文章我们可以调取分类categories下的文章随机生成,实现的方法可以通过 wp_get_post_tags 和 wp_get_post_categories来实现,网上的文章都可能源于露兜博客

相关文章标签tags

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

<ul>
<?php
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
  foreach ($post_tags as $tag) {
    // 获取标签列表
    $tag_list[] .= $tag->term_id;
  }
 
  // 随机获取标签列表中的一个标签
  $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
 
  // 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表
  $args = array(
        'tag__in' => array($post_tag),
        'category__not_in' => array(NULL),  // 不包括的分类ID
        'post__not_in' => array($post->ID),
        'showposts' => 6,                           // 显示相关文章数量
        'caller_get_posts' => 1
    );
  query_posts($args);
 
  if (have_posts()) {
    while (have_posts()) {
      the_post(); update_post_caches($posts); ?>
    <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
    }
  }
  else {
    echo '<li>* 暂无相关文章</li>';
  }
  wp_reset_query(); 
}
else {
  echo '<li>* 暂无相关文章</li>';
}
?>
</ul>

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

相关文章调用分类categories

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

<ul>
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
if ($cats) {
    $args = array(
          'category__in' => array( $cats[0] ),
          'post__not_in' => array( $post->ID ),
          'showposts' => 6,
          'caller_get_posts' => 1
      );
  query_posts($args);
 
  if (have_posts()) {
    while (have_posts()) {
      the_post(); update_post_caches($posts); ?>
  <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
    }
  } 
  else {
    echo '<li>* 暂无相关文章</li>';
  }
  wp_reset_query(); 
}
else {
  echo '<li>* 暂无相关文章</li>';
}
?>
</ul>

本站采用了网上的这两种方式实现了你感兴趣的文章和相关文章的功能。

511遇见不一样的遇见

不一样的遇见技术交流建站免费学习IT的平台

Skip to content

511yj

扫描二维码关注公众号,回复: 11620975 查看本文章

511遇见

不一样的遇见

 您当前的位置: 首页 > Wordpress > 正文内容 WordPress无插件查询tags和categories实现相关文章

WordPress无插件查询tags和categories实现相关文章

wordpress显示文章ID

为了增加阅读量,方便读者找到需要的东西,我们常常在文章的底部添加相关文章的功能,我们可以从两个角度考虑,读者可能感兴趣的文章,以及相关文章,前者我们调取相同 tags下的文章随机生成,而相关文章我们可以调取分类categories下的文章随机生成,实现的方法可以通过 wp_get_post_tags 和 wp_get_post_categories来实现,网上的文章都可能源于露兜博客

相关文章标签tags

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

<ul>
<?php
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
  foreach ($post_tags as $tag) {
    // 获取标签列表
    $tag_list[] .= $tag->term_id;
  }
 
  // 随机获取标签列表中的一个标签
  $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
 
  // 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表
  $args = array(
        'tag__in' => array($post_tag),
        'category__not_in' => array(NULL),  // 不包括的分类ID
        'post__not_in' => array($post->ID),
        'showposts' => 6,                           // 显示相关文章数量
        'caller_get_posts' => 1
    );
  query_posts($args);
 
  if (have_posts()) {
    while (have_posts()) {
      the_post(); update_post_caches($posts); ?>
    <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
    }
  }
  else {
    echo '<li>* 暂无相关文章</li>';
  }
  wp_reset_query(); 
}
else {
  echo '<li>* 暂无相关文章</li>';
}
?>
</ul>

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

相关文章调用分类categories

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

<ul>
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
if ($cats) {
    $args = array(
          'category__in' => array( $cats[0] ),
          'post__not_in' => array( $post->ID ),
          'showposts' => 6,
          'caller_get_posts' => 1
      );
  query_posts($args);
 
  if (have_posts()) {
    while (have_posts()) {
      the_post(); update_post_caches($posts); ?>
  <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
    }
  } 
  else {
    echo '<li>* 暂无相关文章</li>';
  }
  wp_reset_query(); 
}
else {
  echo '<li>* 暂无相关文章</li>';
}
?>
</ul>

本站采用了网上的这两种方式实现了你感兴趣的文章和相关文章的功能。


发布日期: 2016-07-07 作者: 511遇见

所属分类: WordpressWordpress 综合 标签: categories tags WordPress主题开发 分类 标签 相关文章


您可能感兴趣的文章:


▪ 第七课WordPress主题制作综合教程头部Brand设计

▪ Wordpress基于bootstrap自适应主题制作

▪ wordpress让pre支持自动换行

▪ Wordpress主题综合免费视频系列教程

▪ wordpress网站安全的建议和优化

▪ WordPress评论框DIY自定义增加字段

▪ 第五课WordPress主题制作头部文件header.php制作

▪ wordpress邮件地址混淆 你没权限访问整个邮件地址造成的死链接

▪ 第三课511遇见WordPress主题开发教程基本文件的建立

▪ WordPress网站迁移教程 

猜你喜欢

转载自blog.csdn.net/zcp528/article/details/108461371
今日推荐