In Chapter 1, Getting Started: WordPress and jQuery and Chapter 2, Working with jQuery in WordPress we learned how useful it was that jQuery "looped" through the selected elements in the wrapper for you. WordPress does a little looping of its own; in fact, it's important enough to be named "The Loop". The Loop is an essential part of your WordPress theme. It displays your posts in chronological order and lets you define custom display properties with various WordPress template tags wrapped in HTML markup.
The Loop in WordPress is a while loop and therefore starts with the PHP code: while (have_posts()): followed by the template tag the_post(). All the markup and additional template tags are then applied to each post that gets looped through for display. The Loop is then ended with the PHP endwhile statement.
Every template page view can have its own loop so that you can modify and change the look and layout of each type of post sort. Every template page is essentially, just sorting your posts in different ways. For example, different category or tag template pages sort and refine your posts down to meet specific criteria. Those sorted posts can appear different from posts on your main page, or in your archive lists, and so on. The next example is a very simple loop taken from WordPress 2.9.2's default Kubrick theme:
<div <?php post_class() ?> id="post-<?php the_ID(); ?>"> <h2>
<a href="<?php the_permalink() ?>"
rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <?php the_title(); ?>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --> </small>
<?php the_content('Read the rest of this entry »'); ?> </div>
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
The loop is tucked into a large if/else statement that most importantly checks if there are posts to sort. If there are no matching posts to display, a "Sorry" message is displayed, and the searchform.php file is included with the get_search_form() include tag.
The new WordPress 3.0 Twenty Ten theme has its loop separated out into its own template page called loop.php, and it has quite a few more if/else statements within it so that the same loop code can handle many different situations, instead of writing individual loops for different template pages. On the whole, the same basic template tags as well as conditional and include tags are used in the new theme as they were before in the previous default theme. There are now just a few new template and include tags that help you streamline your theme.
Let's take a closer look at some of these template tags, include and conditional tags, and the API hooks available to us in a WordPress theme.
Was this article helpful?
How Any Business Builder Can Turn A Simple Blog Into A Lead Generating, Organization Cash Cow For Any Home Based Business! Everyone knows that in order to succeed in your home based business or your MLM / Network Marketing business, you need to adopt the latest strategies when it comes to making massive money.