- 函数用法
<?php
get_posts( $args );
?>
- $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' );
$posts_array = 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' );
?>
- 刚我们讲到用数组去传参,当然我们也可以用字符串来给该函数传参,下面给一个简单的例子。
<?php
$posts = get_posts('numberposts=3&orderby=rand');
?>
获取分类请看我上一篇文章
好了 以下代码是在获取了分类信息的基础上的
代码是从我的测试成功的完整的代码块中拆出来的,拆出来的代码不保证能完全成功。
function test($categorie_id){
//获取分类id对应分类下的10篇文章
$posts = get_posts( "category=" . $categorie_id ."&numberposts=10" );
if($posts){
foreach( $posts as $post ){
setup_postdata( $post );
$id = $post->ID;
$title = $post->post_title;
//$post_link = the_permalink();
//$post_title = the_title();
$str .= '<p> Post url: '. get_permalink($id) . '</p>';
$str .= '<p> Post title: '. $title . '</p>';
}
}
return $str;
}