WordPress main query function query_posts usage summary

query_posts function in WordPress theme is the control for which articles can appear in the main loop, it may be said that a lot of people do not understand the main loop, so for example, home, these articles archive pages (including pages in) are in the main loop. Query_posts function without the use of control, home page, archive pages are all in accordance with article Published lists all articles published on your blog, and if you want to define which articles can be displayed, which articles do not show, according to what the article kind of sorting method, then you have to use the function query_posts.

WordPress main query function query_posts usage summary

Basic usage:

First, tell us about how to use the query_posts function. Find the archive file in the theme directory page, archive pages include index.php, archive.php, the general category pages, tag pages, date and page of pages, etc. are used archive.php, specific WordPress theme can look at this document constitute articles: WordPress theme files constitute

Determine the list of articles which page you want to control, then we can start, for example, you want the Home articles sorted by number of comments, the basic framework of the code in index.php is this:

  1. <?php
  2.  
  3. // query_posts function
  4. query_posts('orderby=comment_count');
  5.  
  6. // main loop
  7. if ( have_posts() ) : while ( have_posts() ) : the_post();
  8. ..
  9. endwhile; else:
  10. ..
  11. endif;
  12.  
  13. // reset the query
  14. wp_reset_query();
  15.  
  16. ?>

In fact, you have to do is find if (have_posts ()) or while (have_posts ()) in index.php, add query_posts function can be in front. However, the above approach may not lead to home page, you can change the function query_posts this line:

  1. $args = array(
  2. // query_posts parameters, specific parameters can participate in official documents
  3. 'orderby'   => comment_count
  4. ); // the following line of code is necessary, otherwise can not be paged
  5. $arms = array_merge($args, $wp_query->query);
  6. query_posts($arms);

Here are some common query_posts function usage, you can just use your theme.

First, show only articles with a custom field

The most common WordPress site articles sort, usually are arranged in ascending or descending order release time, this sort of almost meet the needs of most sites, but some alternative sites may have different type of requirements: I want to manually modify the articles of arrangement, you can freely change the arrangement position of the article. In fact, the WordPress query_posts can easily solve this problem, but you have to change some code yourself.

Modification method:

1. Open WordPress theme you want to modify the current php file, search code:

  1. while (have_posts())

To replace it:

  1. /**
  2.  * Name: WordPress manually modify the order of articles
  3.  */
  4.  
  5. $args = array(
  6.     'meta_key' => 'sort',
  7.     'the OrderBy' => 'meta_value_num' , // must WordPress 2.8 and above    
  8.     'order' => DESC
  9. );
  10. $arms = array_merge($args, $wp_query->query);
  11.  
  12. query_posts($arms);
  13. while(have_posts())

Then search:

  1. endwhile;

It read:

  1. endwhile;wp_reset_query();

2, all previously published articles to add custom fields (otherwise known as a custom section, you can see at the bottom of the article edit page), the name sort, fill digital value. In this home, the order in accordance with the article on the value of the sort field, in descending order of. Sort by modifying the value of the field, you can achieve the purpose of the order of custom articles.

3, should be noted that, after the adoption of the above modifications, you must add custom fields to sort all articles, otherwise there is no added articles sort field will not be displayed on the home page. Perhaps this is the price to pay to sort manually, or else what is the manual?

4, if you also want the article to manually modify the order tab, category pages and other archive pages, and can also be operated in accordance with the method described in the first step, modify archive.php under the theme catalog.

In fact, this way you can display only seen how I recommend the article, then this custom field contains articles that recommended articles.

Allow visitors to choose their own way of sorting articles

In fact, the implementation process is relatively simple, is to construct a link, the other is to use query_posts to change the main loop on it.

Link structure

The main link for passing GET parameters, so that in the end you want to know PHP program how to sort. Where you need to insert the following code in index.php theme, HTML output for the Sort button. This sort button style, and then write your own css slightly. Note that the following code will automatically get the current user has selected sort order, and give li this sort buttons adds class = "current":

  1. <h4> Articles sort </ h4>
  2. <ul>
  3. <li><a <?php if ( isset($_GET['order']) && ($_GET['order']=='rand') ) echo 'class="current"'; ?> href="<?php echo get_option('home'); ?>/?order=rand" rel="nofollow">随机阅读</a></li>
  4. <li><a <?php if ( isset($_GET['order']) && ($_GET['order']=='commented') ) echo 'class="current"'; ?> href="<?php echo get_option('home'); ?>/?order=commented" rel="nofollow">评论最多</a></li>
  5. <li><a <?php if ( isset($_GET['order']) && ($_GET['order']=='alpha') ) echo 'class="current"'; ?> href="<?php echo get_option('home'); ?>/?order=alpha" rel="nofollow">标题排序</a></li>
  6. </ul>

Change the main loop

First you have to find the following statement in the index.php theme of:

  1. if (have_posts())

Then add the following code before the phrase:

  1. if ( isset($_GET['order']) )
  2. {
  3. switch ($_GET['order'])
  4. {
  5. case 'rand' : $orderby = 'rand'; break;
  6. case 'commented' : $orderby = 'comment_count'; break;
  7. case 'alpha' : $orderby = 'title'; break;
  8. default : $orderby = 'title';
  9. }
  10. global $wp_query;
  11. $args= array('orderby' => $orderby, 'order' => 'DESC');
  12.  
  13. $arms = array_merge($args, $wp_query->query);
  14. query_posts($arms);
  15. }
  16.  
  17. if (have_posts())

Well, it's that simple, copy and paste, easy to implement sorting effect

Second, how to make the article a classification does not display / show

If you do not want to make a classification of the article appeared in the main loop, then you can add parameters category__not_in can use query_posts:

  1. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  2. $args = array(
  3. // 2, 6 is that you do not want to display the classification ID, multiple separated by commas
  4. 'category__not_in'   => array(2, 6),
  5. 'paged' => $paged
  6. );
  7. query_posts($args);

If you only want to display a category of articles, you can category__not_in into category__in. Similarly, if the article is not displayed under a tab, you can change the category__not_in: tag__not_in, or just want to display a label under article can be category__not_in into tag__in, followed by the ID to the tag.

Third, how to sort the article

  1. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  2. $args = array(
  3. // The following code is in the title orderby values, sorted by title
  4. 'orderby'   => title,
  5. 'paged' => $paged
  6. );
  7. query_posts($args);

Depending on the value orderby, allowing articles sorted by number of ways, listed below are a few common values ​​and their corresponding Sort by: title: by title; date: Press release date; modified: by modification time; ID: according to the article ID; rand: random order; comment_count: press reviews

Fourth, to show only the corresponding ID of the article

I just want to show ID, such as 2,4,6 article, you can use the following code:

  1. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  2. $args = array(
  3. ID // The following code is 4, 6 article
  4. 'post__in'   => array(2,4,6),
  5. 'paged'=> $paged
  6. );
  7. query_posts($args);

If you do not want to display these articles 2,4,6, post__in can be changed post__not_in. Also, if want to show sticky posts, it can array (2,4,6) into get_option ( 'sticky_posts'), this code will automatically give you to fill all the ID top of the article.

Fifth, do not let Sticky Sticky article

If you do not want sticky posts at the top, but to give the order they are installed properly, you can use the following code:

  1. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  2. $args=array(
  3. 'paged' => $paged,
  4. 'ignore_sticky_posts' => 1
  5. );
  6. query_posts($args);

Sixth, the article lists all states

WordPress article states there are many, including published, Drafts, Deleted, private, timed release and so on, if you want to have all these articles show up, then it can be:

  1. $paged =(get_query_var('paged'))? get_query_var('paged'):1;
  2. $args = array(
  3. 'post_status' => array('publish', 'pending', 'draft', 'future', 'private', 'trash'),
  4. 'paged' => $paged
  5. );
  6. query_posts($args);

post_status parameter controls specific article, the value of including pending (pending), publish (released), draft (draft), future (timed), private (private), trash (deleted).

Seven, the number of control article

If you want to control the number of articles to display, you can use showposts parameters:

  1. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  2. $args = array(
  3. // display control only 10 articles, 10 if the change of -1 will display all articles
  4. 'showposts' => 10,
  5. 'paged' => $paged
  6. );
  7. query_posts($args);

If you just want to control the number of articles home page, category pages each and every page displayed, you can WordPress management background - Set read the blog page where the number of articles up display - settings.

Guess you like

Origin www.cnblogs.com/nb989/p/11712088.html