WordPress Templates

The WordPress Loop

Without a doubt, the fundamental part of the WordPress system is the loop.

The loop is the code that pulls information from your site’s database. In a default WordPress installation, for example, when your front page loads, the loop asks the database the following questions:

  1. Do you have any posts to display?
  2. If so, can you display them?

This second process typically will be made up of a number of steps.

To get a better sense of this, let’s look at a basic loop from a theme’s index.php file:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <div <?php post_class() ?> id="post-< ?php the_ID(); ?>">

  <h2><a href="<?php the_permalink() ?>">< ?php the_title(); ?></a></h2>

  <div class="entry">
   < ?php the_content(); ?>

  <div class="postmetadata">
    < ?php the_tags('Tags: ', ', ', '<br />'); ?>
    Posted in < ?php the_category(', ') ?> |

  </div>  <!-- END Each Post -->

< ?php endwhile; ?>

< ?php else : ?>
   <h2>Sorry. No posts were found</h2>
< ?php endif; ?>


What Do These Lines Do?

The first line (the have_posts() stuff) tells the database to sequentially make available any available posts.

The second line makes a DIV tag and assigns a CSS ID and class to it.

The third line outputs the title of the post inside a H2 tag—using the WP function the_title().  

The title itself is wrapped in the anchor (‘a’) tag. The destination of the link is provided by the WP function the_permalink(),  which is inserted as the value of the anchor tag’s HREF attribute.

The next interesting line here is the_content(). This appears within a DIV, for formatting purposes. As you might guess, this WP function spits out all the content for the post.

Additional content output by the loop above includes each post’s tags and category (in the above, these appear in the DIV with the class postmetadata.)

Look up a WordPress cheatsheet on the Internet. There are hundreds. For example, one at  NetTutPlus lists additional WordPress functions you can put into your loop, wrapped in the <?php  ?> delimiters:

  • the_excerpt()
  • the_date()
  • the_category()
  • the_author()

Another useful one is the_thumbnail(), which will output each post’s featured image (a featured image can be attached to a post in the post editing part of the dashboard).

The last three parts of the loop are the endwhile, else, and endif sections.

ENDWHILE: The first, endwhile, ends each iteration of the loop. Remember that the loop takes each post, then outputs the information asked of it (such as title, permalink, tags, catagories, author, date, thumbnail, etc).

When we’ve received all the information we want about a post, the endwhile tells the loop to repeat the process with the next available post.

ELSE: This is just the standard fallback clause in any programming language conditional statement. With else, we provide a message if no posts are found.

ENDIF: Finally, we end the IF statement started in the first line of the loop:  if have_posts().