An Introduction to the WordPress Loop
Mar
16
2010
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
- 10 Useful WordPress Loop Hacks
- Get A Permalink Putside of the Loop
- Access Post Data From Outside the Loop
- Only Display Private Posts to Logged Users in the Loop
- Display One Full Post and Three Excerpts
- Calling a WordPress Loop from Inside a WordPress Loop
- Adding Content Between Posts
- Exclude a Category from the WordPress Loop
- Customizing the WordPress Loop for the Single Post
- Redirect Links in WordPress Loop
- Modifying Individual Posts In The Loop
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.















March 16th, 2010 at 6:27 am
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.
March 16th, 2010 at 7:44 am
this is so nice .Thnx Angie