wp_get_recent_posts()获取最近发布的文章

目录

描述

译文

按发表时间检索数据库中的$num最新发表文章。默认检索最近十篇文章。

原文

Retrieve the most recent posts.

wp_get_recent_posts() 描述

用法

<?php wp_get_recent_posts$args$output ?>

Default Usage

<?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$argsARRAY_A );
?>
For a full explanation of the arguments, see get_posts() and the WP_Query object.

wp_get_recent_posts() 用法

参数

$args

(array) (可选)

默认值: array

$output

(string) (可选) Constant OBJECT, ARRAY_A

默认值: ARRAY_A

wp_get_recent_posts() 参数

示例

This is an example that shows how to use the wp_get_recent_posts() function to list the recent 10 posts.

If you want to delimit more or less recent posts you have to put the number in the function parameter like this example below:

To exclude posts with a certain post format, you can use Class_Reference/WP_Query#Taxonomy_Parameters like this next example, which excludes all posts with the 'aside' and 'image' formats:

In the last example you can see the post_title wrapped in a __() function reference,
this is used for internationalization (it's important in case you're dealing with multilanguage site).

wp_get_recent_posts() 示例

注意

  • 使用到: wp_parse_args()
  • 使用到: get_posts()
  • Only the value of ARRAY_A is checked for $output. Any other value or constant passed will return an array of objects.
  • This function returns posts in an associative array (ARRAY_A) format which is compatible with WordPress versions below 3.1. To get output similar to get_posts(), use OBJECT as the second parameter: wp_get_recent_posts( $args, OBJECT );

wp_get_recent_posts() 注意

历史

  • 添加于 版本: 1.0.0
  • 3.1.0: The $num parameter deprecated in favor of $args.

wp_get_recent_posts() 历史

源文件

wp_get_recent_posts() 函数的代码位于 wp-includes/post.php.

wp_get_recent_posts() 源文件