返回介绍

wp_get_recent_posts()

发布于 2017-09-11 12:08:14 字数 7254 浏览 1022 评论 0 收藏 0

wp_get_recent_posts( array $args = array(),  string $output = ARRAY_A )

Retrieve a number of recent posts.


description


参数

$args

(array) (Optional) Arguments to retrieve posts.

Default value: array()

$output

(string) (Optional) The required return type. One of OBJECT or ARRAY_A, which correspond to a WP_Post object or an associative array, respectively.

Default value: ARRAY_A


返回值

(array|false) Array of recent posts, where the type of each element is determined by $output parameter. Empty array on failure.


源代码

File: wp-includes/post.php

function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {

	if ( is_numeric( $args ) ) {
		_deprecated_argument( __FUNCTION__, '3.1.0', __( '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;

}

更新日志

Versiondescription
1.0.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/functions.php: _deprecated_argument()
  • wp-includes/functions.php: absint()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/post.php: get_posts()

Used By

  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mw_getRecentPosts()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mt_getRecentPostTitles()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::blogger_getRecentPosts()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getPosts()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

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

    
    <h2>Recent Posts</h2>
    <ul>
    <?php
    	$recent_posts = wp_get_recent_posts();
    	foreach( $recent_posts as $recent ) {
    		printf( '<li><a href="%1$s">%2$s</a></li>',
    			esc_url( get_permalink( $recent['ID'] ) ),
    			apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
    		);
    	}
    ?>
    </ul>
    
  2. Limit number of recent 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:

    
    <h2>Recent Posts</h2>
    <ul>
    <?php
    	$args = array( 'numberposts' => '5' );
    	$recent_posts = wp_get_recent_posts( $args );
    	foreach( $recent_posts as $recent ){
    		printf( '<li><a href="%1$s">%2$s</a></li>',
    			 esc_url( get_permalink( $recent['ID'] ) ),
    			 apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
    		 );
    	}
    ?>
    </ul>
    

    Exclude posts of a specific post format

    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:

    
    <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 ){
    		printf( '<li><a href=%1$s">%2$s</a></li>',
    			esc_url( get_permalink( $recent['ID'] ) ),
    			apply_filters( 'the_title', $recent['post_title'], $recent['ID'] )
    		);
    	}
    ?>
    </ul>
    

    Limited recent posts thumbnails with captions

    This example can be used to show a limited number of recent posts thumbnails in a slider with captions.
    Sliders uses ids and/or classes on div tags and/or ul tags to apply the custom css and hook up the slideshow in js.
    In this example, we will be using directly the ul tag.

    
    <ul id="slider-id" class="slider-class">
    	<?php
    	$recent_posts = wp_get_recent_posts(array(
    		'numberposts' => 4, // Number of recent posts thumbnails to display
    		'post_status' => 'publish' // Show only the published posts
    	));
    	foreach($recent_posts as $post) : ?>
    		<li>
    			<a href="<?php echo get_permalink($post['ID']) ?>">
    				<?php echo get_the_post_thumbnail($post['ID'], 'full'); ?>
    				//Assuming that the slider support captions 
    				<p class="slider-caption-class"><?php echo $post['post_title'] ?></p>
    			</a>
    		</li>
    	<?php endforeach; wp_reset_query(); ?>
    </ul>
    

    Argument Filering Hook For Widgets

    This function doesn’t have filters but in the default Recent Posts Widget there’s a hook that allows you filter the arguments.

    function filter_recent_posts_widget_parameters( $params ) {
       $params['orderby'] = 'date';
       
       return $params;
    }
    add_filter( 'widget_posts_args', 'filter_recent_posts_widget_parameters' );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文