The WordPress Template Hierarchy Explained

Posted OnNov 14, 2013     CategoryDevelopment     CommentsNo comment

In this article, we’ll be discussing the WordPress Template Hierarchy in a way that aims to make it easy to understand for everyone.

What is the WordPress Template Hierarchy?

The WordPress Template Hierarchy is the hierarchy that dictates what template file, or files, will be used per page generation.

Homepage Generation

When someone visits your WordPress site’s homepage, it will, by default, attempt to create that page first with home.php, if that file exists. If you do not have a home.php (which is extremely common), then WordPress will attempt to generate the page with index.php.

Note that there’s never a question of if there’s an index.php, as it is one of the minimum required files for a WordPress theme to be activated.

Post Generation

Each time a post is requested, WordPress first check to see if the post requested has a defined type, such that if the post type was reggae, it would attempt to load singe-reggae.php.

If such a file doesn’t exist, it will then attempt to load single.php, and should it not find this, it will use index.php

Page Generation

When a page is requested, WordPress will first check if the page requested has a specific page template. If your Theme has a special page for accepting questions it may have a very specific template, such as template-submit-qa.php.

Secondly, it will look if the page has a specific slug and if there’s a template to match. If you have a page about bands and lets say that the slug is also bands, WordPress will try to load page-bands.php

Thirdly, WordPress will check for a page template with an ID, as all pages have an ID, so it’s possible to build specific pages for IDs. If the page has an ID of 221, WordPress will try to load it with page-221.php.

If the first three conditions fail, WordPress will then attempt to generate the page with page.php, and failing that, fall back to index.php

Category Display

The Category process is very similar to that of the Page.

Firstly, WordPress will look for a file that has a matching slug. Such that if you are trying to load the Couch category, it will first try for category-couch.php.

Failing that, it will look for a file where the slug matches the ID of the category, so loading Category 331 will look for category-331.php

Finally, it will attempt to load category.php, then archive.php, then index.php

Leave a Reply

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

Send this to friend