WordPress Template Hierarchy
October 24, 2008
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 Tutorials
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.
WordPress 2.7 PreviewSince I am using WordPress almost exclusively for building my own affiliate sites, I decided to join the WordPress 2.7. beta testers so I could give you a quick overview of what is coming. What I cover here is only a small part of WordPress 2.7., and represents what I found interesting in the hour [...]
Solving WordPress ProblemsWordPress is a stable product, and in most of the cases that I have come across, problems are usually nothing to do with the core files that WordPress distribute. The usual culprit is one or more plugins. Plugins are small pieces of code that add new features to WordPress, and these can be written by [...]
Automatic backups of your WordPress databases?WordPress is a fantastic tool for creating any type of website. However, servers can crash, MySQL databases can get corrupted and gremlins can get into the works! With traditional websites built in HTML, you have always had the option of keeping a backup of your site on your hard disk. With WordPress it’s not so [...]





