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.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<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>

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<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>

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:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<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>

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.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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;

}

wp_get_recent_posts() 源文件