The structure and usage analysis of circulation loop loop in wordpress query _php Tips

Source: Internet
Author: User

WordPress access to the article is the most important loop (loop), in fact, the cycle is to go to the database query to the corresponding article, and then temporarily stored in the global variables inside, the need for an output of an article, WordPress cycle design is very good, complete a cycle need to implement More than 2000 lines of code, and what you see in the loop is just a simple while loop plus several functions that beginners can understand well.

<?php
if (have_posts ()): While
  (Have_posts ()):
    the_post ();
  
  Endwhile;
endif;
? >

Above is a common main loop structure, first use have_posts () function to determine whether there are articles, there is a while statement loop, in the loop inside the call The_posts () function, the_posts () function to tell WordPress core query class, An article has been checked, and every time the_posts () is invoked, the current article in the loop changes.

After several the_posts (), the article has all been cycled, let the have_posts () function return False to end the loop, so a normal loop should be like this:

<?php if (have_posts ()):?>
  <ul>
    <?php while (Have_posts ()): The_post ();? >
      <li <?php post_class ();?>>
        <?php the_title ('  
 

Wp_query class

The method used in the loop is from the Wp_query class, here again to Wp_query class do science:
The functions of the Wp_query class include processing query conditions, retrieving articles from the database, collating and storing articles, looping articles, and judging page types according to the article query criteria.
Property

$query

The incoming query condition.

$query _vars

Handle the available query criteria that $query get.

$queried _object

The query attribution based on the query type. For example, if it is based on a category query, then return a class that contains classified information, and if it is a tab, return the class that contains the label information, the author page, and so on.

$queried _object_id

Returns the ID of the $queried _object. For example, classification is the category ID, the label is the tag ID, and so on.

$posts

The article and article information that is queried from the database is stored in the variable in the form of an array, and each article is an object instantiated from the Wp_post class.

$post _count

The current number of articles, in other words $posts variables, stores several articles.

$found _posts

Statistics how many articles will be available if paging is not enabled.

$max _num_pages

The total number of pages, the current article is divided into several pages.

$current _post

The index value of the current article. In a loop, the start loop $current _post is –1, plus 1 per loop, which represents the index value of the current loop to the article in $posts variable (array).

$post

The current article in the loop is an object that contains the article and article information instantiated through the Wp_post class.

$is _single, $is _page, $is _archive, $is _preview, $is _date, $is _year, $is _month, $is _time, $is _author, $is _category, $is _ Tag, $is _tax, $is _search, $is _feed, $is _comment_feed, $is _trackback, $is _home, $is _404, $is _comments_popup, $is _admin, $ Is_attachment, $is _singular, $is _robots, $is _posts_page, $is _paged

According to the query criteria to determine what the current page, and then stored in some variables, details can refer to the WordPress page judgment function.

Method

Init ()

Initializes the object, all properties are set to NULL,-1, 0, or False, and some properties are deleted.

Parse_query ($query)

Resolves and saves the query string.

Parse_query_vars ()

In the previous version of WordPress and Parse_query () the same functionality, now directly call Parse_query () just fine.

Get ($query _var)

Gets a query condition by name.

Set ($query _var, $value)

Set a query condition.

Get_posts ()

According to the existing query conditions to the database to get the article, and save in the $posts properties, and set the $post _count properties. The return value of this function is the $posts property that stores the article.

Next_post ()

(cyclic control) will $current _post plus 1, which is to push the current article forward, while adding the current post to the $post property, and then return $post properties.

The_post ()

(loop control) into the next article, call the Next_post () function, and then import the current article into the global variable.

Have_posts ()

(circular control) to determine whether the loop is complete or False if all articles are recycled or if there are no articles at all.

Rewind_posts ()

(circular control) jumps to the beginning of the loop. Equivalent to resetting $current _post properties and $post properties.

Query ($query)

Query the article, including calling the Init () method, the Parse_query () method, and the Get_posts () method, and returning the return value of the Get_posts () method, which completes the full operation of an article query.

Get_queried_object ()

The $queried _object property is generated, and $queried _object property is returned directly if it exists, so the $queried _object property must be invoked directly because the default $queried _object does not exist. Queried_object () method.

GET_QUERIED_OBJECT_ID ()

Generates $queried _object_id property and returns $queried _object_id property if it already exists. Similar to the Get_queried_object () method.

Wp_query ($query = '), __constructor ($query = ')

Wp_query class constructor, instantiate the class call itself, if you pass in a query condition, it will automatically call query () method to help you query the article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.