What is The Loop?
You’re probably still wondering what The Loop even is. Basically,it’s what displays the content you see on your
If a user accesses your homepage, archives, or search results – bydefault, the Loop will display a certain number of posts as definedin your Reading Options.
At the moment, my
Basic flow of the loop
Let’s break the Loop down into 3 parts.
<?php if (have_posts()) :?>
<?php while (have_posts()) : the_post();?>
1. What you want displayed in the Loop
<?php endwhile;?>
2. What is displayed when the Loop is over
<?php else : ?>
3. If there’s nothing to display
<?php endif; ?>
If
Template Tags used within the Loop
Unless you want
As you can see, there’s quite a few
- <?php the_permalink()?>
– This will echothe permalink of the post, - <?php the_title();?>
– This echos thepost title, i.e.Hello World! - <?php the_time(‘F jS, Y’)?>
– This will echothe date, i.e.April 4th, 2008. A full list of ways to format the date can befound on php.net - <?php the_author()?>
– This willdisplay the author’s name, i.e.Leland. This is commented out in the default theme. - <?php the_tags(‘Tags: ‘, ‘, ‘, ‘<br/>’); ?>
– This willdisplay the tags assigned to the post, separated by commas, andfollowed by a line break - <?php the_category(‘, ‘)?>
– This willdisplay the categories in a similar fashion as the tags above. - <?php edit_post_link(‘Edit’, ”, ‘ | ‘);?>
– The edit postlink will be visible only to those with permission. - <?php comments_popup_link(‘No Comments »’, ’1Comment »’, ‘% Comments »’); ?>
– Will display thelink to the comments. This will not be displayed on single posts orpages.
There are a lot more listed on the
After the Loop
Let’s take a look at the code after the loop stops looping in thedefault theme.
<div class="navigation">
<divclass="alignleft"><?phpnext_posts_link('« Older Entries')?></div>
<divclass="alignright"><?phpprevious_posts_link('Newer Entries »')?></div>
</div>
As you might have guessed, this will display the pagination you seeon the homepage, archives, and search results. These won’t bedisplayed on single posts and pages. Of course you could replacethis with something like
If there are no posts to display (possibly due to a 404 error), thefollowing will be displayed after the
<h2 class="center">NotFound</h2>
<p class="center">Sorry, but you arelooking for something that isn'there.</p>
<?php include (TEMPLATEPATH . "/searchform.php");?>
That will display the
Template Hierarchy
Some template files will take priority over the index.php forcertain types of pages if they are present in the templatedirectory. Listed below are a few examples of template hierarchy,listed in order of priority.
Homepage
- home.php
- index.php
Single Post
- single.php
- index.php
Search Results
- search.php
- index.php
404 Page
- 404.php
- index.php
There are a few more advanced techniques listed on the
So what’s the point of the template hierarchy? Basically you canuse the it to create new layouts for different types of WordPresspages without hacking up your