An Introduction to the WordPress Loop

We are beginning a new series here on Arbenting for our readers where we take things back to basics, and this post on the WordPress Loop is the first in that Design 101 series. We hope that it is helpful to both beginners and even established pros who are looking for a bit of a refresher. Now on with the show.

The Loop is essentially what WordPress uses to display the posts on your blog to your readers.

Using The Loop, WordPress processes each of the posts to be displayed on the current page and formats them according to how they match specified criteria within The Loop tags. Any HTML or PHP code placed in the Loop will be repeated for each post.

What that is basically saying, is that you can style and designate certain elements for your posts (i.e. title, content, metatags, etc…) via the code, and each time a new post is loaded into the site, those elements are called forth from The Loop to display the posts the same each time.

Below is a breakdown:

Opening the Loop

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

if(have_posts()) checks if there are any posts available

while(have_posts()) begins the loop

the_post() calls up the post and sets the global $post variable

Closing the Loop

<?php endwhile; ?>
 
<?php else : ?>
 
	<p class="no-posts"><?php _e('No posts found'); ?></p>
 
<?php endif; ?>

ends the post loop when wp runs out of posts

follow with what you want to show up if no posts are found

closes the entire loop

A Basic Loop

 
  // Content here will always be displayed
 
<?php if (have_posts()) : ?>
 
  // Content here will be displayed once if there are posts found
 
<?php while (have_posts()) : the_post(); ?>
 
<div class="post" id="post-<?php the_ID(); ?>">
   <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
      <div class="entry">
	  <?php the_content(); ?>
       </div><!-- end entry -->
</div><!-- end post -->
 
<p class="postmetadata">Written on <?php the_time('F j, Y'); ?> at <?php the_time() ?>, by <?php printf( '<a class="url fn" href="' . get_author_posts_url( $authordata->ID, $authordata->user_nicename ) . '" title="' . sprintf( 'View all posts by %s', $authordata->display_name ) . '">' . get_the_author() . '</a>' ) ?><br/>
<?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?><br/>
Category <?php the_category(', ') ?> | Tags: <?php the_tags(' ', ',', ' '); ?></p>
 
<?php endwhile;?>
 
  // Content here will be displayed once when the loop ends
 
<?php else : ?>
 
  // Content here is displayed if no posts are found
 
<?php endif; ?>

Advanced Loops

A Final Word

We appreciate you stopping by and checking out the post. For more from the Arbenting team, make sure you Subscribe for free to the blog.

2 Responses to “An Introduction to the WordPress Loop”

  1. Nice little intro…I remember when I first started creating themes for wordpress trying to find much to get started was difficult…a nice little primer like this would have been great.

  2. this is so nice .Thnx Angie

Leave a Reply

About the Blog

The Arbenting Design Blog was created by Angie Bowen & Rob Bowen to give back to the design community with freebies, how-tos and helpful advice. All of the freebies offered here are available for both commercial & personal use, no attribution required.

Contact Us