The built-in function get_posts of wordpress is used to extract multiple specified articles or random articles in the database. After studying these articles today, it is not difficult to use them, the main method is to parse all functions. First, let's take a look at the usage. The code is as follows:
<? Php $ posts_array = get_posts ($ args);?>
Next let's take a look at $ args parameters:
<? Php
$ Args = array (
// Number of articles to be extracted
"Numberposts" => 10, // start with the number of articles
"Offset" => 0, // category ID. Multiple IDs are separated by commas (,), or an array of IDS is passed. You can specify multiple category IDs.
// The focus of most CMS functions.
"Category" =>, // sorting rule (note 1)
"Orderby" => "post_date", // ascending, descending "ASC" -- Ascending (low to high) "DESC" -- Descending (high to the end)
"Order" => "DESC", // ID of the article to be displayed
"Include" =>, // The ID of the article to be excluded
"Exclude" =>, // custom field name
"Meta_key" =>,
// Set the value of a custom field to the value of the previous parameter.
"Meta_value" =>, // post (log) -- default, page (page ),
// Attachment (attachment), any -- (All)
"Post_type" => "post", // mime type of the article
"Post_mime_type" =>, // The parent ID of the document to be displayed
"Post_parent" =>, // Article status
"Post_status" => "publish ");
?>
Now let's look at its usage:
List of articles from the beginning to the present
If you want to display only one article on the blog homepage, but want to display links to the last five articles in Category ID 1, use the following code:
<Ul>
<? Php
Global $ post;
$ Myposts = get_posts ('numberposts = 5 & offset = 1 & category = 1 ');
Foreach ($ myposts as $ post ):
?>
<Li> <a href = "<? Php the_permalink ();?> "> <? Php the_title ();?> </A>
</Li>
<? Php endforeach;?>
</Ul>
Note: When offset is used, the preceding query applies only to the categories containing more than one article; otherwise, the query cannot be output.
Obtain all document materials
By default, get_posts cannot obtain data related to some articles. For example, you can use the_content () to obtain the article content or sequence ID. Call the internal function setup_postdata () and use the $ post array as its independent variable to solve this problem:
<? Php
$ Lastposts = get_posts ('numberposts = 3 ');
Foreach ($ lastposts as $ post ):
Setup_postdata ($ post );
?>
<H2> <a href = "<? Php the_permalink ();?> "Id =" post-<? Php the_ID ();?> ">
<? Php the_title ();?> </A> <? Php the_content ();?>
<? Php endforeach;?>
You can use $ post-> COLUMN instead of calling setup_postdata () to obtain the ID or content of an article, or getting any relevant data of the article (the data remains in the article list, COLUMN is the COLUMN name of the document data table. Therefore, $ post-> ID indicates the article ID, $ post-> post_content indicates the article content, and so on. To display the data on the page, run the PHP echo command as follows:
<? Php echo $ post-> ID;?>
Sort by title for the latest published articles
The following code displays the date, title, and abstract of the last ten articles in ascending alphabetical order:
<? Php
$ Postslist = get_posts ('numberposts = 10 & order = ASC & orderby = title ');
Foreach ($ postslist as $ post ):
Setup_postdata ($ post );
?>
<Div>
<? Php the_date ();?>
<Br/>
<? Php the_title ();?>
<? Php the_excerpt ();?>
</Div>
<? Php endforeach;?>
Note: The sorting parameters are modified in version 2.6. This code applies to the new sorting format. For details, see parameters.
Any article
You can use the MySQL RAND () function to specify the value of the sorting parameter. The following five articles are displayed:
<Ul> <li> <Ul>
<? Php
$ Rand_posts = get_posts ('numberposts = 5 & orderby = Rand ');
Foreach ($ rand_posts as $ post ):
?>
<Li> <a href = "<? Php the_permalink ();?> "> <? Php the_title ();?> </A> </li>
<? Php endforeach;?>
</Ul>
</Li> </ul>
Show all attachments
You do not need to perform this operation cyclically in the template.
(The get_children () function is relatively convenient after version 2.5 .)
<? Php
$ Args = array (
'Post _ type' => 'attachment ',
'Numberposts' =>-1,
'Post _ status' => null,
'Post _ parent' => null, // any parent
);
$ Attachments = get_posts ($ args );
If ($ attachments ){
Foreach ($ attachments as $ post ){
Setup_postdata ($ post );
The_title ();
The_attachment_link ($ post-> ID, false );
The_excerpt ();
}
}
?>
Show attachments to the latest article
Perform this operation in The_Loop ($ post-> ID available.
<? Php
$ Args = array (
'Post _ type' => 'attachment ',
'Numberposts' =>-1,
'Post _ status' => null,
'Post _ parent' => $ post-> ID
);
$ Attachments = get_posts ($ args );
If ($ attachments ){
Foreach ($ attachments as $ attachment ){
Echo apply_filters ('The _ title', $ attachment-> post_title );
The_attachment_link ($ attachment-> ID, false );
}
}
?>