Forgive me link: http://www.ludou.org/wordpress_query_posts.html
First, we will introduce how to use the query_posts function. Find the archive page file in the topic Directory, which includes index. PHP, archive. PHP. Generally, archive is used for classification pages, tabs, date pages, and author pages. PHP. For more information about the composition of WordPress theme files, see this article: WordPress theme file Composition
Determine the list of articles on which page you want to control, so we can start. For example, if you want to sort the articles on the homepage by the number of comments, then index. the basic framework of PHP code is as follows:
<? PHP // query_posts function query_posts ('orderby = comment_count '); // main loop if (have_posts (): While (have_posts (): the_post ();.. endwhile; else :.. endif; // reset querywp_reset_query ();?>
In fact, you need to find if (have_posts () or while (have_posts () in index. php, and add the query_posts function in front of it. However, the preceding method may cause the homepage to fail to be paged. You can change the query_posts function to the following line type:
// The following line of code is required. Otherwise, your homepage cannot be paginated $ paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1; $ ARGs = array (// Add the query_posts parameter in the following way. For specific parameters, see the official document 'orderby' => comment_count, 'paged' => $ PAGED); query_posts ($ ARGs );
The following are some common query_posts functions. You can directly use them in your topic.
1. Only display articles containing a Custom Field
If you want to display only the articles with a custom field added and sort the articles according to the value of this field, you can refer to this article: WordPress manually modifies the article arrangement order.
In fact, you can see how to display only my recommended articles in this way. The articles containing this custom field are recommended articles.
2. How to disable/display a classified article
If you do not want the articles of a category to appear in the main loop, you can use query_posts to add the category _ not_in parameter:
$ Paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1; $ ARGs = array (// 2, 6 is the category ID you do not want to display, multiple 'category _ not_in '=> array (2, 6), 'paged' => $ PAGED); query_posts ($ ARGs );
If you only want to display an article of a specific category, you can change category _ not_in to category _ in. Similarly, if an article under a tag is not displayed, you can change category _ not_in to tag _ not_in or just want to display the article under a tag, you can change category _ not_in to tag _ in, followed by the tag ID.
Iii. How to sort articles
$ Paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1; $ ARGs = array (// the title in the following code is the value of orderby. Sort 'orderby' => title, 'paged' => $ PAGED); query_posts ($ ARGs );
Different orderby values allow articles to be sorted in many ways. The following lists several common values and their corresponding sorting methods: Title: by title; date: by release date; modified: by modification time; ID: by Article ID; Rand: random sorting; comment_count: By comment
4. show only the articles with the corresponding ID
For example, if you only want to display an article with the ID of 2, 4, and 6, you can use the following code:
$ Paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1; $ ARGs = array (// In the following code, 2, 4, and 6 are the 'post _ in' => array (2, 4, 6), 'paged' => $ PAGED); query_posts ($ ARGs );
If you do not want to display articles 2, 4, and 6, you can change post _ in to post _ not_in. In addition, if you only want to display the top article, you can change array (2, 4, 6) to get_option ('sticky _ posts'). This code will automatically fill in the IDs of all the top posts.
5. Leave the top Article unpinned
If you do not want the top posts to be displayed on the top, but want them to be installed in a normal order, you can use the following code:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;$args=array( 'paged' => $paged, 'caller_get_posts' => 1);query_posts($args);
6. list all status articles
There are many types of WordPress articles, including published, draft, deleted, private, and regularly published articles. If you want to display all these articles, you can:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;$args = array( 'post_status' => array('publish', 'pending', 'draft', 'future', 'private', 'trash'), 'paged' => $paged);query_posts($args);
The post_status parameter can control the status of a specific article, including pending, publish, draft, future, and private ), trash (Deleted ).
7. Control the number of articles
If you want to control the number of articles to be displayed, you can use the showposts parameter:
$ Paged = (get_query_var ('paged '))? Get_query_var ('paged'): 1; $ ARGs = array (// control only displays 10 articles. If you change 10 to-1, all articles are displayed. 'showposts' => 10, 'paged' => $ PAGED); query_posts ($ ARGs );
If you only want to control the number of articles displayed on each page, such as the home page and category page, you can go to the WordPress management background-set-read there to set the maximum number of articles displayed on the blog page.