The ‘Loop’

In a nutshell, in WordPress the ‘loop’ is an if or while statement written in PHP code that says, if something exists, do something. When the loop is executed in WordPress, the browser is looking for posts. If the posts exist, the loop tells the browser how to display them. If no additional posts meeting the criteria exist, the loop is exited and the content is displayed.

Without belaboring the details of the code, it’s important to recognize that all WordPress themes include the loop somewhere in the php files. Many will include it in the index.php and other template files, but some, including Twenty Ten, now include a special loop.php file that is called by other PHP files. This streamlines the code since it doesn’t need to be duplicated in each file, which makes for faster page loading and smaller files, and making the code easier to modify since there’s just the single instance.

Even if you’re not a programmer, it’s good to know that the loop exists, how to recognize it, and understand what it does. On occasion you might want to tweak a little code in a PHP file, and it’s essential that you don’t do anything that breaks the loop, or your site may break.

Most loops are pretty basic, but in Twenty Ten it brings in a lot of additional functionality. To explain it, the code includes the following comment:

In Twenty Ten we use the same loop in multiple contexts. It is broken into three main parts: when we’re displaying posts that are in the gallery category, when we’re displaying posts in the asides category, and finally all other posts.

Additionally, we sometimes check for whether we are on an archive page, a search page, etc., allowing for small differences in the loop on each template without actually duplicating the rest of the loop that is shared.

What does the loop look like? You want to find the part of the code that includes the line:

<?php while ( have_posts() ) : the_post();  ?>

or

<?php if (have_posts()) : while (have_posts()) : the_post();  ?>

and ends with an endwhile statement. In the case of Twenty Ten, the end of the loop looks like this:

<?php endwhile; // End the loop. Whew. ?>

Everything in the middle will be executed over and over, on the condition that there is another post meeting specific criteria (such as all posts within a certain category), and will end when there are no more posts. There may be additional conditional statements as well. For example,

<?php  if ( is_archive() || is_search() ) :   ?>

will only execute whatever follows if it’s a search or archives page.

To put it another way:

Someone wants to read all posts about kittens. The code runs looking for everything with the category “kittens”.

<?php if (( is_category('kittens')  ) {  ?>

would direct the browser to display everything under the category “kittens” as follows:

Enter loop.
Is there a post with the category “kittens”? Yes. Display post “Kittens are cute”.
Is there a post with the category “kittens”? Yes. Display post “Hairballs and you”.
Is there a post with the category “kittens”? No.
End loop, do something else.

If you want more information about this simple but powerful code, visit the WordPress Codex sections on the Loop, Conditional Tags, and Category Templates.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>