The WordPress Loop

Posted: October 3, 2012 in Development, Wordpress


If I’ had to rate the most important Knowledge a WordPress developer should have, I’ would select the WordPress Loop, in this post and answering some request I will dive in to the WordPress Loop. Describing what’s the Loop is all about and how we should use this in our WordPress development.

This post is a first part post of two.

Defining the WordPress Loop

If we want a short description of this feature we may say “that the WordPress Loop  is just a PHP query that allow us to control and talk to the WordPress Content requests, basically is the hearth of WordPress”.

To help us understanding the loop let’s look at a basic usage of the WordPress Loop

A loop is a routine that reference typically not to single Field’s of data but to a collection of data typically post’s, so passing the Pipeline image to the code you find in the WordPress you would have:

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


The rendering of the Posts data

<!--?php endwhile;?>


Actions you want to happen after the ending of the Loop


<!--?php else : ?>


When there is no data associated with the Loop sentence


<!--?php endif; ?>

We have describe the Basic Loop. The Basic Loop checks whether there are any posts to render, the number of element’s shown by the loop is controlled in the global blog settings.

When exists post’s to render the While Loop is started, and as long the number of post’s rendered is less or equal then the number defined in the settings the different post’s are iterated.

Best Practice note

In the beginning of WordPress and even today many Themes authors define the Loop of their Themes as part of the Index.PHP template file, and in the other Template files that have the need to show some Post contents. I personal recommend you to isolate the loop logic from the Theme presentation creating a separated  file where you will define the Loop template. To include the loop in to the Template file use the code i.e

<!–?php get_template_part (‘loop’,’index’); ?>

What will happen is that when processing the Index.php template the runtime will look for the Loop definition in to the loop-index.php.

The main propouse of defining a Template Loop is to control the loop in a specific case. If you don’t want to control that, the WordPress will only revert for the WordPress base loop that lives in to the loop.php file.

Using the Loop

Before we start to talk about the Loop control usage and the different way we can explore this, we need to take a look in to the “kernel” of the Loop WP_Query.

WP_Query is explained in the codex and is the class responsible for querying for post’s or some post’s information.

When in the above example we had use the instruction have_posts() what in reality we have done was $wp_queryàhave_posts(). Why this is important? Because knowing this means that you can have different Loop’s in the same template file and that you can also have multiple Loops running at the same time i.e

<!–?php $news_query = new WP_Query() ?> using now the news_query we can trigger a new Loop.

If you wish to use the Loop as a way to choose part’s of your posts to display or to define the way the post’s should behave, after knowing what’s the loop and that you can use the WP_Query as a way to define “subLoops”, the next set of tools you need to know are named as template tags. And what Template Tags can be used control the post output.

Template Tags  are defined by the Codex as WordPress instructions that allow us to do or get something (to know more about template tags worth to go to

The most common Template tags when you want to control a post rendering in the loop are:

<!–?php the_permalink() ?> – This will echo the permalink of the post, i.e

<!–?php the_title(); ?> – This echos the post title, i.e. Hello World!

<!–?php the_time(‘F jS, Y’) ?> – This will echo the datethe list of ways to format the date can be found on

<!–?php the_author() ?> – This will display the author’s name

<!–?php the_tags(‘Tags: ‘, ‘, ‘, ‘
’); ?> – This will display the tags assigned to the post, separated by commas, and followed by a line break

<!–?php the_category(‘, ‘) ?> – This will display the categories

<!–?php edit_post_link(‘Edit’, ”, ‘ | ‘); ?> – The edit post link will be visible only to those with permission.

<!–?php the_content_link(); ?> – Shows the content of the post.

Image now that you wish to override the Loop in order to get the following effect

Then your code would be similar with

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

      <a href=”<?php the_permalink();?>” title=”<?php the_title()?>”/>

<!--?php endwhile;?>


<!--?php endif; ?>

In next part of this post we will look to other types of post, and how we can build multiple Loops and what are the cases in. We will also continuing to look to the Loop backstage magic and see the alternative to the loop usage.


To my references go to


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s