WordPress built-in function get_posts () is used to invoke the specified one or more articles, the function provides several parameters, you can set the invocation of the article of the specified classification, custom fields, article type, article status, display order and other conditions, the article is returned as an array, the output can be through the loop statement.
Code structure:
1 |
<?php get_posts ($args);? > |
Returns the variable of the array type, $args is the necessary variable for the function
Variable $args parameter:
1234567891011121314151617181920212223242526272829303132333435363738394041 |
<?php $args = Array ( //number of articles displayed ' numberposts ' = +// with the first article as the starting position ' offset ' = 0, //Call the category ID, multiple separated by commas, or pass a numbered array, you can specify multiple classification numbers. ' category ' = +, //article Sorting rules ' ORDER BY ' = ' post_date ' , //Ascending, descending ' ASC '--ascending (low to high) ' desc '--descending (high to bottom) ' order ' = ' DESC ', //To show the article ID ' include ' and //To exclude the ID of the article ' Exclude ' + , //custom field name ' Meta_key ' + , //custom field value, with previous parameter, To select an article that displays values that match a custom field. ' Meta_value ' + , //post (log)-Default, page (pages), attachment (attachment), any--(All) ' post_type ' = > ' post ', //MIME type of the article ' post_mime_type ' = =/ /To display the article's parent ID ' post_parent ' = , //Article Status ' post_status ' = ' publish ');? > |
Ordered by order optional values:
- ' Author '--Sort by author numeric number
- ' Category '--Sort by category numeric number
- ' Content '--Sort by content
- ' Date '--Sort by creation date
- ' ID '--Sort by article number
- ' Menu_order '--Sort by menu order. Only pages are available.
- ' Mime_type '--Sort by MIME type. Only attachments are available.
- ' Modified '--sort by last modified time.
- ' Name '--sort by stub.
- ' Parent '--Sort by Parent ID
- ' Password '--Sort by password
- ' Rand '--any sort of result
- ' Status '--Sort by status
- ' title '--Sort by title
- ' Type '--Sort by type
Example: calling four articles with the specified ID 1,3,4,6
1234567891011121314 |
<?php $posts = get_posts ("numberposts=4&post_type=any&include=1,3,4,6"); if ($posts): foreach ($posts as $post): Setup_postdata ($post);? ><li> |
Get_posts () function WordPress calls the specified ID article multiple articles