Loop There It Is

Reading Time: 3 minutes

The Loop is the heart of WordPress. Open up index.php in twentyfifteen and The Loop will be staring you right in the face. Learn The Loop and you’ll know the backbone of WordPress. Woah.

What exactly is The Loop? Let’s go to the Codex for help.

The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.

That’s really good and all, but to a non-developer that may be hard to understand. The Loop really breaks down into three essential parts. if ( have_posts() ) , while ( have_posts() ) , and the_post(); . Let’s break it down.

if ( have_posts() )

What’s the first thing we need to do before we try to pull in post data? Why, check to see if we even have posts of course. So we holler down the hall to Trevor, the new intern1, and ask him to check if we have any posts. Ten minutes pass and you go down to find Trevor Googling “what is a post”.

WordPress makes this scenario a lot easier using a common PHP statement. The if statement. Then WordPress has a handy dandy little function to see if we have any posts the have_posts() function. Which checks to see if our query has any data to throw back our way.

Put those two together and WordPress runs down the hall, grabs all the posts we have, and is back faster than Trevor looking up from his iPhone. That’s going to look like this.

<?php if ( have_posts() ) : ?>

<?php endif; //make sure to close your if statement ?>

while ( have_posts() )

Now WordPress is standing in the doorway holding all of our posts. While she’s got them might as well throw them up on the website. So the next thing we’ll do is set up a PHP while loop to run while we have_posts().

Let’s add that into our if statement.

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

     <?php endwhile; //also make sure to close your while statements ?>
<?php endif; //make sure to close your if statements ?>

the_post();

Any code we put within our while loop is going to be run for each post our query pulls in. But the first thing we want to run within our while loop is the_post(); function.

This function iterates the post index and grabs the next post, sets up the post data, and let’s WordPress know that we are within The Loop. Once the_post(); is run we can use all of those fancy functions like the_title(), the_permalink(), the_post_thumbnail(), and more to display our posts plethora of data.

Put that all together and you’ve mastered The Loop.

<?php if ( have_posts() ) : ?>
     <?php while ( have_posts() ) : the_post(); ?>
          
          <?php //pull in all that awesome post content ?>
          
     <?php endwhile; //also make sure to close your while statements ?>
<?php endif; //make sure to close your if statements ?>

There you have it. You’ve got all of your post data at your fingertips faster than Trevor could spill piping hot coffee all over himself.

Bonus Loop Variations

There’s more than one way to write the loop. Here’s a few.

The Codex

<?php 
if ( have_posts() ) {
	while ( have_posts() ) {
		the_post(); 
		//
		// Post Content here
		//
	} // end while
} // end if
?>

The Minimalist

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

<?php endwhile; endif; ?>

The twentyfifteen

<?php if ( have_posts() ) : ?>

     <?php while ( have_posts() ) : 
          
          get_template_part( 'content', get_post_format() );
          // that's a call to another php file with your post styles and data
     endwhile; 
else :
     get_template_part( 'content', 'none' );
     // php file if we have no posts
endif;
?>
  1. Brad’s nephew.

Pin It on Pinterest