学会使用get_posts();方法调用文章并且显示
这个方法文件所在位置为:wp-includes/post.php
这个方法是wordpress的内置函数,可以用它提取多篇指定或随机文章。
$args是该函数必要的变量。
get_posts($args)将返回数组型的变量。
其中这个数组中所包含的东西:引用链接
<?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’ —— 按类型排序
随机获取3个文章的代码示例。
<? $rand_posts = get_posts('numberposts=3&orderby=rand'); ?>
<ul>
<?foreach ( $rand_posts as $post ) {
setup_postdata( $post ); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<? } wp_reset_postdata(); ?>
</ul>