This article mainly introduces how to use PHP functions used for obtaining recent articles in WordPress development, that is, how to use the wp_get_recent_posts function, for more information, see the wp_get_recent_posts function. in WordPress, a function can obtain the array of recent articles. compared with other methods, wp_get_recent_posts returns an array instead of an object, therefore, it may be more convenient for new users or lazy people to use. let's explain this function as an example.
Description
Wp_get_recent_posts
The English explanation is very short, so I will not post it,
Obtain the function of the latest article.
In fact, this is another use of the get_posts function.
The description can be used in a good way.
Usage
<?php wp_get_recent_posts( $args ) ?>
Default value
<? Php $ args = array (// Number of articles Retrieved 'numberposts' => 10, // Obtain the 'offset' => 0 from the first few articles, // The Category ID, if this parameter is not set, 'category' => 0 is displayed for all categories, // sorting rules (note 1, Details) 'orderby' => 'Post _ date ', // sort 'order' => 'desc' in ascending or descending order, // include the article ID 'include' =>, // exclude article ID 'exclude' =>, // the custom field name 'meta _ key' =>, // The value of the custom field. in combination with the previous parameter, select an article that matches the custom field value. 'Meta _ value' =>, // Article type: article or page 'post _ type' => 'post', // Article status: draft, published, and hidden articles .... 'Post _ status' => 'Draft, publish, future, pending, private', // This wood study 'SS Ress _ filters '=> true);?>
Instance
Because it is similar to get_posts,
Therefore, we will not give any advanced examples, but simply give an official example.
Recent Posts
<? Php // Get the latest article $ recent_posts = wp_get_recent_posts (); // traverse each article. Foreach ($ recent_posts as $ recent) {echo'
- '. $ Recent ["post_title"].'
';}?>
Summary
Note: Because the return value of the wp_get_recent_posts function is an array,
In theory, the setup_postdata function can only accept variables of the object type,
Therefore, according to the official documentation, the setup_postdata function cannot be used to load data to the loop.
I didn't try it because I didn't use much. if you are interested, you can try it out.
Therefore, if you use the setup_postdata function,
The wp_get_recent_posts function is not as convenient as the get_posts function.
Note:
'Author' -- sort by author value
'Category '-- sort by category value
'Content' -- sort by content
'Date' -- sort by creation date
'Id' -- sort by document number
'Menu _ order' -- sort by menu order. Only available on pages.
'Mime _ type' -- sort by mime type. Only attachments are available.
'Modified' -- sort by the last modification time.
'Name' -- Sort By stubs.
'Parent' -- sort by parent ID
'Password' -- sort by password
'Rand' -- any sorting result
'Status' -- sort by status
'Title' -- sort by title
'Type' -- sort by type