Template Hierarchy

In WordPress, different files will be called upon to render a web page depending on what is known as the ‘template’, or file, hierarchy.

As explained in the WordPress Codex,

Templates are then chosen — and web page content is generated — in the order suggested by the WordPress Template hierarchy, depending upon what templates are available in a particular WordPress Theme.

When rendering a page, WordPress will look for template files with specific names in the current Theme’s directory and uses the first matching template file listed under the appropriate query section below.

In a nutshell, there are a number of different kinds of PHP files that may be called depending on which kind of page has been requested, and these different files will render different information on the page.

At its most basic, a WordPress theme requires just two files to function: an index.php file and a style.css file. However, most websites will have separate pages for the archives, a 404 ‘page not found’ page, an author information page, the home page, etc. Each of these will typically include the navigation menu and header/footer but the body of the page may be rendered somewhat differently. Some may not have a sidebar, or will pull specialized content such as all posts for a specified category.

If a template file exists to render this content, WordPress will display it; if not, it will follow a defined sequence to find an appropriate php file to render, and if it cannot find one, it will default to index.php.

The easiest way to understand this process is to examine the files of a theme.

For example, in the Twenty Ten theme you have the following files:

  • 404 Template (404.php)
  • Archives (archive.php)
  • Attachment Template (attachment.php)
  • Author Template (author.php)
  • Category Template (category.php)
  • Comments (comments.php)
  • Footer (footer.php)
  • Header (header.php)
  • Main Index Template (index.php)
  • One column, no sidebar Page Template (onecolumn-page.php)
  • Page Template (page.php)
  • Search Results (search.php)
  • Sidebar (sidebar.php)
  • Single Post (single.php)
  • Tag Template (tag.php)
  • Theme Functions (functions.php)
  • loop.php (loop.php)
  • sidebar-footer.php (sidebar-footer.php)

Some of these files are basic structural files that will be called for every page in the theme (the header, footer, loop). Depending on whether the particular page is a regular “page” (not a blog post), or the blog, or an author page, or the 404, etc. a different file will be called to display the content, and the formatting of those pages will look a bit different so that the specific content can be displayed appropriately.

For example, in Twenty Ten the 404 page (aka “file not found” page) has a static statement that the content was not found, and offers the visitor the chance to search again by displaying the search box in the main part of the page. It also does not display comments (try it on this page – go to the search box in the sidebar and type in some random text to force a file not found error).

While the 404 page doesn’t need to look different from a regular page, it makes sense to display slightly different content. However, if no 404.php existed, WordPress would default to the main index.php page.

This diagram from the WordPress Codex explains the hierarchy (click on image to enlarge it):

template hierarchy flowchart

Digging into WordPress also has a nice diagram showing which file in a template will be called by WordPress.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>