描述:
获取最近发布的文章
用法:
<?php wp_get_recent_posts( $args, $output ) ?>
默认用法:
<?php
$args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
有关参数的完整解释,请参见get_posts()和WP_Query对象。
参数:
$args
(array) (可选) 查询条件数组
默认值: array
$output
(string) (可选) 常量对象, ARRAY_A
默认值: ARRAY_A
示例:
使用wp_get_recent_posts()函数列出最近10篇文章
<h2>Recent Posts</h2>
<ul>
<?php
$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a> </li> ';
}
?>
</ul>
如果您想限定最近的文章,您必须在函数参数中输入数字,如下例所示:
<h2>Recent Posts</h2>
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a> </li> ';
}
?>
</ul>
要排除具有特定文章形式的文章,可以使用类“Reference/WP”“Query”“Taxonomy”“参数,如下例所示,它排除具有“aside”和“image”格式的所有帖子:
<h2>Recent Posts</h2>
<ul>
<?php
$args = array( 'numberposts' => '5', 'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-aside',
'operator' => 'NOT IN'
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-image',
'operator' => 'NOT IN'
)
) );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '">' . ( __($recent["post_title"])).'</a> </li> ';
}
?>
</ul>
源文件:
/**
* Retrieve a number of recent posts.
*
* @since 1.0.0
*
* @see get_posts()
*
* @param array $args Optional. Arguments to retrieve posts. Default empty array.
* @param string $output Optional. Type of output. Accepts ARRAY_A or ''. Default ARRAY_A.
* @return array|false Associative array if $output equals ARRAY_A, array or false if no results.
*/
function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
if ( is_numeric( $args ) ) {
_deprecated_argument( __FUNCTION__, '3.1', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) );
$args = array( 'numberposts' => absint( $args ) );
}
// Set default arguments.
$defaults = array(
'numberposts' => 10, 'offset' => 0,
'category' => 0, 'orderby' => 'post_date',
'order' => 'DESC', 'include' => '',
'exclude' => '', 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true
);
$r = wp_parse_args( $args, $defaults );
$results = get_posts( $r );
// Backward compatibility. Prior to 3.1 expected posts to be returned in array.
if ( ARRAY_A == $output ){
foreach( $results as $key => $result ) {
$results[$key] = get_object_vars( $result );
}
return $results ? $results : array();
}
return $results ? $results : false;
}