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<wbr></wbr>homepage,your<wbr></wbr>singleposts,<wbr></wbr>pages,archives, search results, and more.
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<wbr></wbr>homepage<wbr></wbr>displays10 posts per page, which is what I defined<wbr></wbr>Showat most * posts. On single posts and pages – the samebasic Loop code will just display<wbr></wbr>just<wbr></wbr>thatspecific page.
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<wbr></wbr>there are postsavailable in the query, it will start displaying them ina<wbr></wbr>while<wbr></wbr>loop,what is defined in<wbr></wbr>part1. When the while is over, it will display what isin<wbr></wbr>part2. If there’s no posts found, or there’s some sort ofother 404 error,<wbr></wbr>part3<wbr></wbr>getsdisplayed.
Template Tags used within the Loop
Unless you want<wbr></wbr>1.What you want displayed in the Loop<wbr></wbr>repeated on yourWordPress blog’s homepage 10 times, you should probably learn someof the basic template tags. Let’s take a look at the codeof<wbr></wbr>index.php<wbr></wbr>inthe default WordPress template.
As you can see, there’s quite a few<wbr></wbr>templatetags<wbr></wbr>within the Loopthat will output things such as the post title, the permalink, thecontent, etc. I’ll break down each of the template tags in theWordPress default theme.
- <?php the_permalink()?><wbr></wbr>– This will echothe permalink of the post,<wbr></wbr>
- <?php the_title();?><wbr></wbr>– This echos thepost title,<wbr></wbr>i.e.Hello World!
- <?php the_time(‘F jS, Y’)?><wbr></wbr>– This will echothe date,<wbr></wbr>i.e.April 4th, 2008. A full list of ways to format the date can befound on<wbr></wbr>php.net
- <?php the_author()?><wbr></wbr>– This willdisplay the author’s name,<wbr></wbr>i.e.Leland. This is commented out in the default theme.
- <?php the_tags(‘Tags: ‘, ‘, ‘, ‘<br/>’); ?><wbr></wbr>– This willdisplay the tags assigned to the post, separated by commas, andfollowed by a line break
- <?php the_category(‘, ‘)?><wbr></wbr>– This willdisplay the categories in a similar fashion as the tags above.
- <?php edit_post_link(‘Edit’, ”, ‘ | ‘);?><wbr></wbr>– The edit postlink will be visible only to those with permission.
- <?php comments_popup_link(‘No Comments »’, ’1Comment »’, ‘% Comments »’); ?><wbr></wbr>– Will display thelink to the comments. This will not be displayed on single posts orpages.
There are a lot more listed on the<wbr></wbr>TemplateTags<wbr></wbr>page over atWordPress.org. Some of these may work in the Loop, while some maynot.
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<wbr></wbr>PageNavi,but that’s up to you.
If there are no posts to display (possibly due to a 404 error), thefollowing will be displayed after the<wbr></wbr>else
<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<wbr></wbr>NotFound<wbr></wbr>message along withthe search form. In this case there would have to be a search formcode located in a file called<wbr></wbr>searchform.php<wbr></wbr>inthe template directory, which there is in the default theme.
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<wbr></wbr>TemplateHierarchy<wbr></wbr>page over atWordPress.org.
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<wbr></wbr>index.php<wbr></wbr>filetoo much.