探索建站和运维乐趣

根据标签/作者/分类相关调用 实现调用WordPress相关文章

有些时候我们在WordPress主题中并没有调出相关的文章功能,于是我们希望通过插件或者代码实现。调出相关文章会有很多种可能,比如是按照相关分类、相关标签、同作者的相关文章。这个需要我们根据需要进行选择。在这篇文章中,我们整理到支持分类、标签、作者等相关调用文章的方法,你需要哪个就选择哪个。

第一、标签相关文章

<ul id="tags_related">
<?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>

第二、分类相关文章

<ul id="cat_related">
<?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>

第三、分类和标签相关

<h3>Related Posts</h3>
<ul>
<?php
$post_num = 5; // 数量设定.
$exclude_id = $post->ID; 
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) {
  $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ','; //zww: edit
  $args = array(
          'post_status' => 'publish',
          'tag__in' => explode(',', $tags), // 只选 tags 的文章.
          'post__not_in' => explode(',', $exclude_id), // 排除出现过的文章.
          'caller_get_posts' => 1,
          'orderby' => 'comment_date', // 依评论日期排序.
          'posts_per_page' => $post_num
  );
  query_posts($args);
  while( have_posts() ) { the_post(); ?>
             <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
     <?php
          $exclude_id .= ',' . $post->ID; $i ++;
  } wp_reset_query();
}
if ( $i < $post_num ) { // 当tags的文章不足, 再取 category .
  $cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
  $args = array(
          'category__in' => explode(',', $cats), // 只选 category 的文章.
          'post__not_in' => explode(',', $exclude_id),
          'caller_get_posts' => 1,
          'orderby' => 'comment_date',
          'posts_per_page' => $post_num - $i
  );
  query_posts($args);
  while( have_posts() ) { the_post(); ?>
             <li><a rel="bookmark" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php $i++;
  } wp_reset_query();
}
if ( $i  == 0 )  echo '<li>没有相关文章!</li>';
?>
</ul>

第四、作者相关文章调用

<ul id="author_related">
<?php
  global $post;
  $post_author = get_the_author_meta( 'user_login' );
  $args = array(
        'author_name' => $post_author,
        'post__not_in' => array($post->ID),
        'showposts' => 6,               // 显示相关文章数量
        'orderby' => date,          // 按时间排序
        '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();
?>
</ul>

本文有参考 neo 相关的内容。如果我们有需要使用的时候,根据需要调用,然后修改相应的排版和样式。

投上你的一票
赞(0)
转载保留:老部落 » 根据标签/作者/分类相关调用 实现调用WordPress相关文章


关注公众号『老蒋朋友圈』

获取更多建站运营运维新知!
互联网创业、前沿技术......