WordPress Template Hierarchy
October 24, 2008 by Andy
WordPress theme templates explained
When you look at the files that come with WordPress templates, it can be very confusing. Different templates may or may not have a certain file. Let’s take a look at an example. Here are the template files from a theme called “librio” and one of the revolution templates.
I have highlighted the files that are present in the Revolution theme but not in the Librio theme. If we work out way down the list, the following files are present in the Revolution theme, but not in the Librio theme:
- breadcrumb.php
- home.php
- page_archive.php
- page_blog.php
- sidebar_left.php
- sidebar_page.php
- sidebar_post.php
- sidebar_right.php
- tag.php
The first one – breadcrumb.php is a file created to insert the breadcrumb navigation into the pages of the theme and is called from other wordpress templates. Similarly, the 4 sidebar templates in the Revolution theme are simply there to offer greater customisation of the sidebars. Since the sidebars are again called from other templates, this is again not a template hierarchy issue.
The other files highlighted above (home.php, page_archive.php, page_blog.php and tag.php) are templates of one form or another. So, why does the Revolution theme have these extra templates?
Well, there is a hierarchy involved in the selection of templates to display certain pages. As an example, if the tag.php template is present, it will be used in the construction of the tag pages. If it is not present, the in archive.php file will be used instead.
Here is the hierarchy:
If we take the first line of this diagram, the homepage will be created from the home.php file if it exists, otherwise it will create the homepage from the index.php file.
The second line – a post will be created using the single.php file if it exists, and if not, it too will use the index.php file.
If you look at the third line – pages – it explains why the Revolution theme was the following two files:
These files are simply the templates to be used for page archives, and blog pages respectively.
The tag.php template is there to be used for the tag pages in the Revolution theme. Its not present in the Librio theme, so in that case, tag pages will be built in the Librio theme using the archive.php file which does exist.
So, the reason some themes appear to be missing certain files is simply because there are other template files that can pick up the slack and be used instead.
This is great to know because it means you can easily modify your existing templates by adding new template files. e.g. suppose you want to add some code to the end of your posts (like you would if you were adding “similar posts” code). If your template did not have the file single.php, it means that you could add this code to the index.php file. However, this code will then be used in ALL pages that are built with the index.php file, and that can include quite a few. So, a better option would be to make a copy of your index.php file, and call it single.php. You could then edit the single.php file so that the “similar posts” code is only used in the posts. Cool eh?
Print This Post
Related Articles
- Choosing a WordPress Theme - Changing your WordPress theme is very easy, so you can have some fun downloading and changing the look and feel of your site. There are a few things you should look out for when choosing a theme. Ideally you should have some posts already on your blog, as well as a static page, some comments e.t.c.
- Removing the Gravatar Link from Revolution Template Themes - The Gravatar link has been shown to be a common way my sites are losing visitors. In this tutorial, I’ll show you how to remove that link. Bookmark on Delicious Digg this post Recommend on Facebook Buzz it up Buzz it up share via Reddit Share with Stumblers Tweet about it Subscribe to the comments on this post
- WordPress Tutorial – Adding Similar Posts - Its a great idea to add similar posts to your WordPress sites, as it encourages site stickiness, as well as giving pages on your site an SEO boost. Not only is the page then linking to other related content on your site, but the pages it links to now have inbound links from relevant
- WordPress posts & pages – what are the differences? - WordPress gives you the option of using a post or a page for your content. There is often a lot of confusion anongst WordPress users as to which one they should use and why. Bookmark on Delicious Digg this post Recommend on Facebook Buzz it up Buzz it up share via Reddit Share with Stumblers Tweet about it Subscribe to the comments on
- Creating Custom sidebars in WordPress - Have you ever wanted to have certain ads only show if the sidebar of certain pages? WordPress uses a template system that means every page uses the same sidebar, but we can add code to the sidebar to test if a page is in a certain category and then insert code if it






This post is great, even by your high standards. I’ve long been confused by how WordPress templates work and never understood the explanations. Thanks a lot.
Pearson
Andy,
Great explanation of templates, which I could never fully understand. You amaze me at how good you are are explaining things, Whether it be keyword research, lsi, wordpress, or reviewing a products – you have the gift of teaching. It’s a pleasure reading your newsletter every week.
Sorry, i do not get it. I cannot see in the hierarchy diagramm why page_archive.php or page_blog.php should be used and when. So which template has to be absent, that these templates are used instead?
Yes it is confusing, especially when templates dont appear to belong to the normal WordPress template set. page_archive.php and page_blog.php (found in some StudioPress templates) are page template files, that can be applied to WordPress pages. The naming of these files is chosen by the WordPress designer, who in this instance uses the prefix page_ to signify that the template relates to pages.
O.K Andy, lets see:
So that means, if you create a new page, wordpress is using a default template for that page. What wordpress chooses as the default template depends on the already existing template files in your theme directory;
for instance, according to the template hierarchy, if page.php does exist, WordPress uses it as the default template. If page.php would not exist, index.php would be the default instead.
When you create a new page and you select a custom template (you already made), than the page is designed according to this custom template. This page template is now on the top of the hierarchy. If this file would be missing somehow, page.php drops in, etc…
Exactly.