wordpress loop

The WordPress Loop

The WordPress Loop is PHP code utilized by WordPress to show posts. Utilizing The Loop, WordPress processes every post to be displayed on the present page, and codecs it in respond to the way it matches specified standards inside The Loop tags. Any HTML or PHP code within the Loop shall be processed on every submit.

When WordPress documentation says “This tag should be inside The Loop”, comparable to for particular Template Tags or plugins, the tag shall be repeated for every submit. For instance, The Loop shows the next info by default for every submit:

Title (the_title())
Time (the_time())
Classes (the_category()).

You possibly can show different details about every submit utilizing the suitable Template Tags or (for superior customers) by accessing the $submit variable, which is about with the present submit’s info whereas The Loop is working.

For a newbie’s have a look at The Loop, see The Loop in Motion.
Utilizing the Loop

The Loop must be positioned in index.php and in some other Templates used to show submit info.

You should definitely embody the decision for the header template on the prime of your Theme’s templates. In case you are utilizing The Loop inside your individual design (and your individual design isn’t a template), set WP_USE_THEMES to false:

That is utilizing PHP’s various syntax for management buildings, and may be expressed as:


Loop Examples

This instance shows every submit with its Title (which is used as a hyperlink to the Put up’s Permalink), Classes, and Content material. It additionally permits posts in a class with Class ID ‘three’ to be styled in another way. To perform this, the in_category() Template Tag is used. Learn the feedback fastidiously to see what every a part of the code is doing.


Be aware: All HTML code should be exterior the tags. And, PHP code (even issues so simple as curly braces: } ) should be contained in the tags. You can begin and cease blocks of PHP code to intersperse HTML code inside if and else statements, as proven within the above instance.
Exclude Posts From Some Class

This instance demonstrates the right way to conceal a particular Class or Classes from being displayed. On this case, posts from Classes three and eight are excluded. The instance is completely different than the instance above in that it makes a change to the question itself.


[WooZoneProducts asin=”0596516177″][/WooZoneProducts]


Be aware: For those who use this instance in your primary web page, it’s best to use a distinct Template in your Class archives; in any other case, WordPress will exclude all posts in Class three and Class eight, even when viewing that Class Archive! Nonetheless, if you wish to use the identical template file, you possibly can keep away from this by utilizing the is_home() tag to make sure that posts from Class three and Class eight will solely be excluded from the principle web page:


There are different Conditional Tags that can be utilized to manage the output relying on whether or not or not a specific situation is true with respect to the requested web page.
A number of Loops

This part offers with superior use of The Loop. It’s kind of technical, however don’t let that scare you. We’ll begin with a straightforward instance and work up from there. With a little bit widespread sense, endurance, and enthusiasm, you can also do a number of loops.

First off, why would one need to use a number of loops? Usually, the reply is that you just would possibly need to do one thing with one group of posts, and do one thing completely different to a different group of posts, however show each teams on the identical web page. “One thing” might imply virtually something; you might be solely restricted by your PHP ability and your creativeness.

We’ll get into examples under, however first it’s best to learn concerning the fundamentals. Check out the essential Loop. It consists of:


In English (PHP varieties and other people conversant in code converse can skip to under), the above could be learn: If we’re going to be displaying posts, then get them, separately. For every submit within the record, show it in line with . Once you hit the final submit, cease. The do stuff line(s), are template dependent.

Somewhat apart on Do stuff: on this instance it’s merely a placeholder for a bunch of code that determines the right way to format and show every submit on a web page. This code can change relying on the way you need your WordPress to look. For those who have a look at the Kubrick theme’s index.php the do stuff part could be every part under:


A proof for the coders on the market: The have_posts() and the_post() are comfort wrappers across the world $wp_query object, which is the place the entire motion is. The $wp_query is named within the weblog header and fed question arguments coming in by means of GET and PATH_INFO. The $wp_query takes the arguments and builds and executes a DB question that leads to an array of posts. This array is saved within the object and likewise returned again to the weblog header the place it’s stuffed into the worldwide $posts array (for backward compatibility with previous submit loops).

As soon as WordPress has completed loading the weblog header and is descending into the template, we arrive at our submit Loop. The have_posts() merely calls into $wp_query->have_posts() which checks a loop counter to see if there are any posts left within the submit array. And the_post() calls $wp_query->the_post() which advances the loop counter and units up the worldwide $submit variable in addition to the entire world submit information. As soon as we’ve exhausted the loop, have_posts() will return false and we’re performed.
Loop Examples

Under are three examples of utilizing a number of loops. The important thing to utilizing a number of loops is that $wp_query can solely be known as as soon as. With a view to get round this it’s potential to re-use the question by calling rewind_posts() or by creating a brand new question object. That is coated in instance 1. In instance 2, utilizing a variable to retailer the outcomes of a question is roofed. Lastly, ‘a number of loops in motion’ brings a bunch of concepts collectively to doc a technique of utilizing a number of loops to advertise posts of a sure class in your weblog’s homepage.