get_posts() 用于在 WordPress 中提取多篇文章。

调用

<?php $posts = get_posts($args); ?>

参数

<?php
$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => ,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' => ,
'post_type' => 'post',
'post_mime_type' => ,
'post_parent' => ,
'post_status' => 'publish' );
?>

参数具体含义

<?php
$args = array(
//需要提取的文章数
'numberposts' => 10,

//以第几篇文章为起始位置
'offset' => 0,

//分类的ID,多个用逗号将分类编号隔开,或传递编号数组,可指定多个分类编号。
//大部分 CMS 使用该函数的重点。
'category' => ,

//排序规则(注1)
'orderby' => 'post_date',

//升序、降序 'ASC' // 升序 (低到高) 'DESC' // 降序 (高到底)
'order' => 'DESC',

//要显示文章的ID
'include' => ,

//要排除文章的ID
'exclude' => ,

//自定义字段名称
'meta_key' => ,
//自定义字段的值,配合上一个参数,来选择显示符合自定义字段数值的文章。
'meta_value' => ,

//post(日志)//默认,page(页面),
//attachment(附件),any // (所有)
'post_type' => 'post',

//文章的 mime 类型
'post_mime_type' => ,

//要显示文章的父级 ID
'post_parent' => ,

//文章状态
'post_status' => 'publish' );
?>

注1:

'author' // 按作者数值编号排序
'category' // 按类别数值编号排序
'content' // 按内容排序
'date' // 按创建日期排序
'ID' // 按文章编号排序
'menu_order' // 按菜单顺序排序。仅页面可用。
'mime_type' // 按MIME类型排序。仅附件可用。
'modified' // 按最后修改时间排序。
'name' // 按存根排序。
'parent' // 按父级ID排序
'password' // 按密码排序
'rand' // 任意排序结果
'status' // 按状态排序
'title' // 按标题排序
'type' // 按类型排序

另外 query_posts()get_posts() 函数作用类似,但wordpress官方提示 query_posts()有可能会扰乱 WordPress 主循环,不推荐使用。

如果要在文章循环之外想要将查询内容显示出来,需要用到setup_postdata()这个函数。

标签: