WordPress在文章页获取作者的相关文章的方法

作者:袖梨 2022-06-25

WordPress中的文章页我们可能需要获取到作者的相关文章,推荐给作者更多阅读的内容,我们可以使用query_posts()自定义Loop循环,在文章页有一个全局的$post变量,通过$post->post_author获取作者的ID,所以获取相关作者的文章,功能代码如下:


  • <?php

  • $args = array(

  • 'post__not_in' => array($post->ID),

  • 'showposts' => 6, // 显示相关文章数量

  • 'orderby' => date, // 按时间排序

  • 'post_author' => $post->post_author

  • );

  • query_posts($args);

  • if (have_posts()):

  • while (have_posts()):the_post(); update_post_caches($posts); ?>


  • <?php endwhile; else: ?>
  • 暂无相关文章

  • <?php endif; wp_reset_query(); ?>

相关文章

精彩推荐