The WordPress Loop

The WordPress loop is used in templates to bring in the content from your pages or posts. It is written in PHP. The loop can be modified in a variety of ways. For example one might want to show excerpts instead of full posts, only show posts from a certain category, etc. In the case of single pages or posts, a basic version of the loop will usually suffice.

WordPress pages, posts, and archives all use different variations of the loops, all of which can be customized to suit your needs. The launch of WordPress 3.0 earlier this year also offers additional options, but for the purposes of this presentation we are sticking with very basic examples. If you have downloaded a frameworked theme such as Thesis or a newer theme such as TwentyTen, your code could be different than the examples used on this site.

Here is the Loop we are using on this page

Note: The div's for blogpost and blogpostwrapper are optional, but may be useful if you want to style some post elements differently than others.

<div class="blogpostwrapper">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="blogpost">
<h3 class="entrytitle" id="post-<?php the_ID(); ?>"> <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?> </a> </h3>
<?php the_content(); ?>
</div>

<?php endwhile; ?>
<?php else : ?>
<h6 class="center">Not Found</h6>
<p class="center">Sorry, but you are looking for something that isn't here.</p>

<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
</div>

WordPress Loop Resources