WordPress Template Hierarchy: The Complete Guide

One of WordPress’ best features is its powerful templating capabilities. By manipulating a handful of files, you can create complex themes with different layouts and visual options for different types of content. This is made possible by WordPress’ complex template hierarchy which gives the designer immense control over content presentation and layouts.

What is Template Hierarchy in WordPress?

To understand template hierarchies, we must first understand how WordPress renders pages.

Let’s consider that you have a WordPress blog, CoolBlog.com. You want to access a certain page on your blog with the URL:

http://coolblog.com/category/cool-posts/

When you type this URL into your browser, WordPress will swing into action to determine what type of page is being requested. WordPress has different pages for different content types – an archive page, a home page, a search page, etc. Once WordPress determines the page-type, it will try to find a template for that particular page.

In this example, WordPress knows that you are requesting a category page (since there is a /category/ right in the URL). But you aren’t requesting just any category page – you are requesting a specific category, cool-posts. Thus, WordPress will try to find a template that corresponds to this specific category.

Let’s break this down into individual steps:

WordPress will first look for a template for that particular category. Each category has a unique ID. In the above example, if the category ID is 5, it will try to find a template file named category-5.php

If WordPress can’t find the category-5.php file, it will move up one level and use the category.php template to render the page.

If the category.php template isn’t available, WordPress will use the archive.php template (since a category is nothing but a specific type of archive).

If archive.php can’t be found, it will fall back on the default index.php template.

Thus, the progression is from specific to general templates. For the above example, this can be represented as follows:

There are different hierarchies for different page types. For example, if you have your website front page at CoolBlog.com, WordPress will take the following path to determine which template to use:

WordPress will first look for front-page.php template which will serve as the front page.

If WordPress can’t find front-page.php, it will look for the template for the next level – home.php.

If it can’t find home.php, it will finally resort to using the default index.php.

No matter the content type, WordPress will always use index.php if it can’t find any other template. This means that you could have a fully functional (albeit with poor usability) website with just the index.php template file.

WordPress Template Hierarchy by Content Type

WordPress can automatically detect what content type needs to be shown and search for the appropriate content type. Below, we’ll take a look at the hierarchies for a few selected content-types:

Category Pages

As mentioned above, a category is a special type of archive. WordPress utilizes the following hierarchy when displaying category pages:

If you have a specific category slug or name (say, cool-posts.php), WordPress will make use of it first. You can edit category slugs in the category page in your WP dashboard.

Each category also has a category ID, regardless of whether you’ve given it a slug or not. WordPress creates the category ID automatically. If WP can’t find the category slug, it will use the category ID next, like category-32.php

If the category ID template is missing, WordPress will use the default category.php template.

If it can’t find a category template, it will use the basic archive.php template.

If it can’t find even the archive.php template, it will turn to the index.php template.

Conclusion

WordPress gives you immense control over the visual presentation of different content types. By creating custom templates for specific content-types, you can completely alter the look of your website. This is great for theme designers, coders and even lay users who want a high-degree of control over their blog’s presentation.