說明
wordpress 按發表時間檢索數據庫中的最新發表文章。默認檢索最近十篇文章。
用法
- <?php?wp_get_recent_posts(?$args?)??>
參數
- <?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
- ????);
- ?>
或者傳入一個(整數)將獲取的文章數量
默認值: 10
返回的值
(數組) 跟 get_posts 不同get_posts 返回的是 objects
文章列表
示例
1.返回最新的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"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
- }
- ?>
- </ul>
2.返回最新的5篇文章
- <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"])?.?'"?title="Look?'.esc_attr($recent["post_title"]).'"?>'?.???$recent["post_title"].'</a>?</li>?';
- ?}
- ?>
- </ul>
修改記錄
自1.1.0版本后
源文件
wp_get_recent_posts() is located in wp-includes/post.php.
- 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?)?);
- ?}
- ?
- ?$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?);
- ?
- ?if?(?ARRAY_A?==?$output?){
- ??foreach(?$results?as?$key?=>?$result?)?{
- ???$results[$key]?=?get_object_vars(?$result?);
- ??}
- ??return?$results???$results?:?array();
- ?}
- ?return?$results???$results?:?false;
- }