Understanding the WordPress Loop

Each year a new edition of WordPress or two comes out and with it a brand new theme to showcase the capabilities of the updated WordPress installation. Many things have changed over the years but one thing continues to stay the same – the WordPress Loop and its integral role in making this the most preferred platform of all.

For anyone working on WordPress with the intent to make beautiful themes or simply achieve a certain task, the loop makes it worthwhile. Without this nifty piece of code, WordPress simply will not exist. Whether you are displaying a page, a post, a category page, an archive or a custom post type – a WordPress loop is at the core of things. An example of a WordPress Loop in action can be found here on the Mirador Wealth Blog: http://www.miradorwealth.com.au/blog/

Okay, show me the WordPress Loop already!

Right so, now that you have drilled it into your head that the WordPress Loop is life and death for your website, time to find its home and dissect it further. Let’s begin with what it is. Now, the loop is actually just a few lines of codes mostly involving PHP and HTML. The PHP code lets you query the database and find the content you are searching for while the HTML decorates this content up the way you wish it to be.

Being a loop, it naturally repeats itself over a few times depending on the content it retrieves from the database. Once all content has been displayed, it stops. Hence, when you are dealing with a single page or post or a static content, the loop runs just once but for your blog page, archive and category, it goes on repeating until all the content is displayed on the screen.

Accordingly, the kind of content displayed by the WordPress loop depends on a list of things.

  1. If any customization has been done to the basic loop.
  2. The HTML wrapper used for the retrieved content.
  3. Functions used to retrieve data from the database and display them.
  4. The type of content retrieved.

If you look at any WordPress theme, there is always a loop that plays an integral part in all template files. Either it exists as part of the template file itself, in which case there will be plenty of repeated loop codes or it exists as a template itself that is then repeatedly called.

Okay enough of talk, let’s see what this loop actually is.

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

Do something here using HTML and retrieved post metadata

<?php endwhile ?>

<?php endif; ?>

The first line of code begins the loop and it basically asks the WordPress database if there are any posts matching a specific condition. If a single post content is returned then the code moves to the next segment, which is the while block and this is the WordPress loop. The while command basically asks the database to return results one at a time until all results matching previous query have run out.

Inside the code block, you can do a lot of things like using HTML to decorate the content or using WordPress functions to output specific pieces of post metadata and much more.

Finally, you close the loop followed by the if block.

This folks is the basic WordPress loop in action. As always, here’s the official loop page from the WordPress Codex.

Leave a Reply

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