If you want to customize themes or build new ones from scratch, it’s important to understand how the WordPress template hierarchy works.
First and foremost, this WordPress system helps keep everything organized. Once you know what all the theme template files are, what they do, and how they’re prioritized, you’ll be able to alter almost every aspect of how your WordPress website looks.
In this article, we’ll explain what the WordPress template hierarchy is and how it works. Then we’ll provide a full breakdown of the template files involved in each type of WordPress page (including a cheat sheet for reference), to help you use them to your advantage.
Let’s get to it!
An Introduction to the WordPress Template Hierarchy
Typically, non-dynamic websites use static HTML and CSS files to render their contents. However, WordPress is a dynamic platform based on the PHP programming language. Every WordPress site loads multiple .php files, each one governing how a specific section or component looks.
Whenever you load a certain type of page, the Content Management System (CMS) looks for the template files that correspond to it.
For example, if you use WordPress’ search feature and load a search results page, the CMS will look for two template files:
- search.php, which governs how your search results pages look
- index.php, which is the default template file that WordPress uses when it can’t find the top option within each hierarchy
The template files you have access to will depend on what theme you use. All WordPress themes are a collection of templates, stylesheets, and other elements, such as images. So in the above example, if the theme you use includes a search.php template, WordPress will find and load it.
In some cases, you might use a theme that doesn’t include template files for the types of pages you want to load. That’s where the WordPress template hierarchy comes in. It’s a built-in system that tells WordPress what template files to load and in which order.
For a search page, if WordPress can’t find the search.php file, it will move down to the next file in the hierarchy, which is index.php. This file is the final fallback for every single branch within the template hierarchy.
In theory, you can have a fully functional theme that only includes one template file, and that is index.php.
In practice, though, a theme with a single template file would barely include any style customizations at all, and every type of page would look pretty much the same. Unless that’s what you’re going for, understanding the WordPress template hierarchy is one of the most important steps you can take as a theme developer.
How the WordPress Template Hierarchy Works
As you may know, WordPress lets you use multiple types of pages depending on what you want to publish. There are seven main categories you can use:
This article was written by Matteo Duò and originally published on Blog – Kinsta.