WordPress的Get_Posts()函数详解

WP中获取POST有两个主要函数,Get_post()和Get_Posts()。一个是获取单文章,另外一个是获取多文章,其中,官网对Get_posts()函数的描述很简单。但有的时候描述越简单的函数,使用起来却并没有那么简单。下面我通过一个案例来解释一下WP_Posts()函数。
下面这个案例,是获取页面上那篇文章的分类,然后显示这个分类下的5篇文章。显示最新的5篇。
代码之中Categories是数组,获取分类信息的。然后得到这个数组中的文章。随后开始使用Get_Posts()方法。获取到文章的篇数和确定该文章属于特定的分类。
  1. <?php/*single page?show current category articles*/?>
  2. <?phpif ( is_single() ) :global $post;
  3. $categories = get_the_category();
  4. foreach ($categories as $category) :    ?>    
  5. <li class="widget widget_recent_entries" id="<?php $category->term_id;?>-posts">        
  6. <h2 class="widgettitle"><?php echo $category->name; ?></h2>        
  7. <ul>        
  8. <?php        $posts = get_posts('numberposts=5&category='. $category->term_id);    
  9.     foreach($posts as $post) :        ?>            
  10. <li>            
  11. <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>            </li>       
  12.  <?php endforeach; ?>        </ul>    </li>
  13. <?phpendforeach; endif ; ?>
  14. <?php/*end show current category articles*/?>

其中,numberposts=5&category='. $category->term_id,是在获取本分类下的5篇文章。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

基于这段代码,结合Get_Posts()函数,我们可以对这种获取进行更细化的操作。比如获取随机的文章,或获取最早、最晚的文章。都是可以的。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

首先看一下Get_Posts()函数的官方说明。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

get_posts 函数详解

该函数属于 WordPress 的内置函数,用于在 WordPress 中提取多篇指定或随机文章。
使用方法:文章源自原紫番博客-https://www.yuanzifan.com/53255.html

  1. <?php
  2. $args = array(
  3. 'numberposts' => 5,
  4. 'offset' => 0,
  5. 'category' => ,
  6. 'orderby' => 'post_date',
  7. 'order' => 'DESC',
  8. 'include' => ,
  9. 'exclude' => ,
  10. 'meta_key' => ,
  11. 'meta_value' => ,
  12. 'post_type' => 'post',
  13. 'post_mime_type' => ,
  14. 'post_parent' => ,
  15. 'post_status' => 'publish' );
  16. $posts_array = get_posts( $args );
  17. ?>

$args是该函数必要的变量,也就是该函数的参数。
  get_posts( $args )将返回数组型的变量。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

  1. <?php
  2. $args = array(
  3. //需要提取的文章数
  4. 'numberposts' => 10,
  5.  
  6. //以第几篇文章为起始位置
  7. 'offset' => 0,
  8.  
  9. //分类的ID,多个用逗号将分类编号隔开,或传递编号数组,可指定多个分类编号。
  10. //大部分 CMS 使用该函数的重点。
  11. 'category' => ,
  12.  
  13. //排序规则(注1)
  14. 'orderby' => 'post_date',
  15.  
  16. //升序、降序 'ASC' —— 升序 (低到高) 'DESC' —— 降序 (高到底)
  17. 'order' => 'DESC',
  18.  
  19. //要显示文章的ID
  20. 'include' => ,
  21.  
  22. //要排除文章的ID
  23. 'exclude' => ,
  24.  
  25. //自定义字段名称
  26. 'meta_key' => ,
  27. //自定义字段的值,配合上一个参数,来选择显示符合自定义字段数值的文章。
  28. 'meta_value' => ,
  29.  
  30. //post(日志)——默认,page(页面),
  31. //attachment(附件),any —— (所有)
  32. 'post_type' => 'post',
  33.  
  34. //文章的 mime 类型
  35. 'post_mime_type' => ,
  36.  
  37. //要显示文章的父级 ID
  38. 'post_parent' => ,
  39.  
  40. //文章状态
  41. 'post_status' => 'publish' );
  42. ?>

变量参数详解

上面介绍了默认的数组中的类型,其中比较重要的是排序,即Orderby。Wordperss官方给出的参数是以下这些:
‘author’ —— 按作者数值编号排序
‘category’ —— 按类别数值编号排序
‘content’ —— 按内容排序
‘date’ —— 按创建日期排序
‘ID’ —— 按文章编号排序
‘menu_order’ —— 按菜单顺序排序。仅页面可用。
‘mime_type’ —— 按MIME类型排序。仅附件可用。
‘modified’ —— 按最后修改时间排序。
‘name’ —— 按存根排序。
‘parent’ —— 按父级ID排序
‘password’ —— 按密码排序
‘rand’ —— 任意排序结果
‘status’ —— 按状态排序
‘title’ —— 按标题排序
‘type’ —— 按类型排序文章源自原紫番博客-https://www.yuanzifan.com/53255.html

实例刚我们讲到用数组去传参,当然我们也可以用字符串来给该函数传参,下面给一个简单的例子。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

需要注意的是,如果单纯想要用升序或者降序,只使用Order函数即可。Orderby这个函数的值不可以等于desc或者asc,那样会报错。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

下面几个简单的例子,帮大家理解Get_Posts()函数。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

  1. //显示随机的3篇文章
  2. <?php
  3. $posts_rand = get_posts('numberposts=3&orderby=rand');
  4. ?>
  5.  
  6. //时间顺序从早到晚显示10篇文章
  7. <?php
  8. $posts_ten = get_posts('numberposts=10&order=asc');
  9. ?>
  10. //显示10篇文章,但是排除分类序号为12的文章
  11. <?php
  12. $posts_excupost = get_posts('numberposts=10&order=asc&exclude=12');
  13. ?>

希望以上几个实例,可以帮你更好的了解Wordpress的Get_Posts()方法。文章源自原紫番博客-https://www.yuanzifan.com/53255.html

原梓番博客原创,转载注明出处。

需要Wordpress技术支持的可以点这里:WordPress主题插件修改配置

站长微信
扫码添加(注明来意)
weinxin
Yuanzifan99
原梓番博客公众号
博客内容精选
weinxin
原梓番博客
 

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证
加载失败