Bookmark and Share

Contemplating the Template Hierarchy

Wednesday, October 7th, 2009 by MikeLT

As I have previously mentioned, up to this point the only template files in this themes folder were the index, header, footer, and sidebars. If you were to look into the default theme directory, you would see that there are many other possibilities for template files that allow us to style our themes at a very granular level. Category, archives, authors, posts, and pages can all have templates that set them apart from each other both visually and contextually.

Wordpress automatically gives certain file names preference over others, and if a particular file is not found in the theme directory, it will use the next file in line, ending up at index.php if no other choice is available.

Before we delve further into the template hierarchy, it is important that we understand the most important part of the Wordpress platform, the part that allows us to differentiate between these files, and the engine behind it all: a relatively small piece of coding known as “the loop“.

Understanding the Wordpress Loop

In the middle of every template file is the PHP code that tells the hosting server what information to retrieve from the MySQL database, and how to present that information via CSS.

The Wordpress Loop begins with:

<?php if (have_posts()) : ?>
/*if statement determines if there are any posts*/

<?php while (have_posts()) : the_post(); ?>
/*while statement: as long as there are posts, run ‘em*/

…and ends with:

<?php endwhile; ?>
/*ends the while statement*/

<?php endif; ?>
/* ends the if statement, effectively ending the loop*/

Everything in between is what is being fetched for that particular template page, which as theme designers is up to us to decide.

<?php get_header(); ?>
<div id=”content”>

<?php if (have_posts()) : ?>

<?php while (have_posts()) : (the_post()); ?>

<?php endwhile; ?>

<?php endif; ?>

</div>

<?php include (TEMPLATEPATH . ‘/sidebar1.php’); ?>
<?php include (TEMPLATEPATH . ‘/sidebar2.php’); ?>
<?php include (TEMPLATEPATH . ‘/topnav.php’); ?>

<?php get_footer(); ?>

In the case of this theme, the anatomy of a template file would consist of fetching the header template file, then establishing the content div id, and starting the Loop. Once the Loop has completed, the content div is closed, and the two sidebars are called, where they execute their own loops. I also include a navigation php file at this point that is placed at the very top, even above the header, through the miracle of CSS.

Lastly, the footer is called on to execute its loop, which ends in </body></html>, effectively ending the whole process from an html point of view.

And that my friends, is what is happening in a Wordpress Blog.


Resources: The Loop in Action – Straight from the horse’s mouth, the Wordpress Codex.

Bookmark and Share

Leave a Reply

You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

Host Gator hosting service provider - unlimited domains