In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use the cyclic Loop structure of query articles in WordPress. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
WordPress to get the article is the most important cycle (Loop), in fact, the loop is to go to the database to query the corresponding article, and then temporarily stored in the global variables, when needed, the output of an article out, WordPress loop design is very good, to complete a cycle requires the execution of more than 2000 lines of code, and you use the loop to see a simple while loop plus several functions, beginners also understand.
The above is a common main loop structure, first use the have_posts () function to determine whether there is an article, and then use the while statement to loop, call the the_posts () function in the loop body, and the the_posts () function is used to tell the WordPress core query class that it has already checked an article, and every time the_posts () is called, the current article in the loop will change.
When the_posts () is called several times and the article has finished the loop, let the have_posts () function return False to end the loop, so a normal loop should look like this:
WP_Query class
All the methods used in the loop come from the WP_Query class, so let's do some popular science on the WP_Query class:
The functions of the WP_Query class include processing query conditions, going to the database to get articles, sorting and storing articles, article loops, and judging page types according to article query conditions.
Attribute
$query
The query criteria passed in.
$query_vars
Process the available query conditions obtained by $query.
$queried_object
The query attribution given according to the query type. For example, if the query is based on classification, return a class that contains classification information; if it is a tab, return a class that contains tag information; author pages, and so on.
$queried_object_id
Returns the ID of $queried_object. For example, classification is classification, ID; tag is tag ID;, and so on.
$posts
Articles and article information queried from the database are stored in this variable as an array, and each article is an object instantiated from the WP_Post class.
$post_count
The current number of articles, in other words, several articles are stored in the $posts variable.
$found_posts
Count how many articles there will be if paging is not enabled.
$max_num_pages
Total number of pages, the current article is divided into several pages.
$current_post
The index value of the current article. In the loop, the starting loop $current_post is-1, plus 1 for each loop, which represents the index value of the article to which the current loop is in the $posts variable (array).
$post
The current article in the loop is an object that contains articles and article information instantiated by 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 conditions to determine what the current page, and then stored in some variables, details can refer to the "WordPress page judgment function".
Method
Init ()
Initialize the object, set all properties to null,-1, 0, or False, and remove some properties.
Parse_query ($query)
Parse and save the query string.
Parse_query_vars ()
With the same functionality as parse_query () in previous WordPress versions, it would be nice to call parse_query () directly now.
Get ($query_var)
Get a query condition based on the name.
Set ($query_var, $value)
Set a query condition.
Get_posts ()
Go to the database to get the article according to the existing query conditions, save it in the $posts property, and set the $post_count property. The return value of this function is the $posts property that stores the article.
Next_post ()
(loop control) add $current_post to 1, that is, push the current article forward, add the advanced current article to the $post property, and then return the $post property.
The_post ()
(loop control) go to the next article, call the next_post () function, and then import the advanced current article into the global variable.
Have_posts ()
(loop control) determines whether the loop is complete, and returns False if all articles have finished or there are no articles at all.
Rewind_posts ()
(loop control) skip to the beginning of the loop. This is equivalent to resetting the $current_post property and the $post property.
Query ($query)
Query the article, including calling the init () method, the parse_query () method, and the get_posts () method, and return the return value of the get_posts () method. This function can be used to complete the complete operation of an article query.
Get_queried_object ()
Generate the $queried_object attribute and return the $queried_object attribute directly if it already exists, so since the default $queried_object does not exist, you must directly use the get_queried_object () method to invoke the $queried_object attribute.
Get_queried_object_id ()
Generate the $queried_object_id attribute, and return the $queried_object_id attribute directly if it already exists. Similar to the get_queried_object () method.
WP_Query ($query =''), _ _ constructor ($query ='')
The constructor of the WP_Query class is called when you instantiate the class. If you pass in a query condition, it will automatically call the query () method to query the article for you.
This is the end of the article on "how to use the cyclic Loop structure of query articles in WordPress". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.