If you want to access the full potential of WordPress, it helps to know a little about templates. When WordPress renders a page, it uses templates to determine how everything should look. Also, the platform adheres to a strict hierarchy that keeps things organized. Know what the WordPress template hierarchy is (WordPress Template Hierarchy) and how it works will allow you to customize your website theme in more detail.
The WordPress template hierarchy is organized into seven main categories:
- Site home page
- individual tickets
- individual pages
- Category and tag pages
- Custom Post Types
- Search results pages
- 404 error pages
In this guide, we will introduce you to WordPress templates and their hierarchy. We’ll also explain how themes use template files to display your website pages. We have a long way to go, so let’s get down to business!
Introduction to Template Files (and how they relate to WordPress themes)
When you create a basic static website, you will normally just use HTML and CSS to render it and customize its appearance. WordPress, on the other hand, is much more powerful. The platform is based on and uses various files .php to determine how each aspect of your pages appears.
Take a look at the right side of this post, for example. There is a sidebar that shows you other related posts from our blog. When your browser told WordPress to load this post, it also brought up several , one of which is called sidebar.php. That file includes information about how to display the sidebar you’re currently viewing, and what elements it should include:
Sidebars are, of course, only part of what makes up a full page. Most WordPress pages require multiple template files to work, including the following:
- index.php
- header.php
- sidebar.php
- footer.php
- functions.php
- single.php
- comments.php
Please note that these are just some of the template files you will find in WordPress. header.php, sidebar.php Y footer.php they are particularly important, as they are what are known as ‘template partials’. This means that they can be integrated into many other templates.
WordPress’ approach to rendering pages may seem complex at first. However, it is actually quite efficient. If you had a unique template for each page of your website, customization would be a nightmare. WordPress’ modular approach to templates allows you to make changes to a single file and use that element throughout your website wherever you need it.
When it comes to choosing which templates to use for each page, the first thing WordPress will do is check which theme you’re using. Each theme includes its own set of template files that will take precedence over all others. That’s part of what we call the WordPress “Template Hierarchy” in action, which is something we’re going to explore in the next section.
Now that you understand how WordPress renders your pages, you might start to understand that themes are basically a collection of template files. In practice, a theme only needs one template file to work, and that is index.php. However, most themes include many more templates. For everything a theme doesn’t include, WordPress looks to other files in its hierarchy to fill in those gaps.
How WordPress Hierarchy Works for Templates
In the previous section, we introduced you to some examples of WordPress template files. However, those are just some of the templates that can be used when a page or post is loaded. The WordPress Hierarchy is what determines which templates are used and in what order.
If you try to load the page of a hypothetical category of hostingfor example, this is what happens in the background:
- WordPress will look for a template file called category-hosting.php in your current theme directory.
- If there is no file category-hosting.phpWordPress will look for one that uses the category ID instead, like category-2.php.
- If WordPress doesn’t find any of those options, it will look for a generic file category.php instead.
- If you can’t find a file called category.phpWordPress will try again and look for the template archive.php.
- Finally, if all else fails, the platform will upload the file index.php of your theme and use it as the page template.
Certain template files always take precedence over others, which is why they are organized in a hierarchy. Generally speaking, WordPress websites are made up of seven categories of pages, each with its own tightly defined hierarchy. Next, let’s explore what those categories are and how their hierarchies work.
Breaking Down the WordPress Template Hierarchy (7 Categories)
As far as WordPress is concerned, all websites can be divided into seven types of pages. Each of these categories has a built-in WordPress template hierarchy, which we’ll take a step-by-step look at below.
1. Main page of the site
First, let’s talk about the front page of your website. When WordPress loads your home page, the first thing it will look for is a file front-page.php. If that file is not available, the platform will resort to home.php. If both files are missing, WordPress will fall back to the always reliable file index.phpwhich is always there (otherwise your theme won’t work).
In other words, this is how this particular hierarchy breaks down:
- front-page.php
- home.php
- index.php
Even if these three files are exactly the same, WordPress will follow its internal logic. This particular hierarchy is quite simple, of course. Now, let’s dive into a page type that is a bit more complex.
2. Individual tickets
WordPress posts (like the one you’re reading) fall under the single post category. Near the beginning of this section, we talked about some of the template files involved in rendering an input. However, those were mostly internal items. Before WordPress can render them, it must determine which template file to use for the page as a whole.
This is how the hierarchy of individual entries works:
- single-{post-type}-{slug}.php
- single-{post-type}.php
- single.php
- singular.php
- index.php
You probably don’t recognize some of those template files, so let’s give them some context. The first on the list is single-{post-type}-{slug}.php. A more practical example could be single-product-ca-12.php, in the case of a virtual store. To put it another way, WordPress will look for a unique template file for each post you upload within its specific category. If the platform cannot find a relevant template, it will fall back to single-{post-type}.php and so on, until it inevitably comes back to index.php.
In practice, this approach allows you to design custom templates for individual posts or products. However, if you prefer to use a single template for all your posts, that’s what this is for. single.php.
3. Individual pages
Individual pages have their own category with WordPress. For example, let’s take the website as a whole. https:// is our home page, and when accessed it loads the template front-page.php. Other sections of the website, such as https:///hosting-webthey fall into the category of individual pages.
An individual page follows this hierarchy:
- Custom template file
- page-{slug}.php
- page-{id}.php
- page.php
- singular.php
- index.php
You’ll notice that the first item in the list is not a filename. This is because WordPress can recognize various types of content as individual pages. For example, if you’re working with a post, WordPress will default to the hierarchy we discussed earlier. Individual pages, on the other hand (such as /web-hosting) will go directly to page-slug.php. In our example, this would be page-hosting-web.php (if such a file exists).
From there, this hierarchy works the same as with inputs. If there is no template file for a page’s unique slug, WordPress will look for one that matches its ID, and so on. As always, all lines lead to index.php at the end, if you don’t get off at an earlier station.
4. Category and tag pages
As you may recall, we actually covered category hierarchy earlier in this article as an example. In any case, we recapitulate the templates that this hierarchy covers, in order:
- category-{slug}.php
- category-{id}.php
- category.php
- archive.php
- index.php
This hierarchy works the same as for individual posts and pages. WordPress will search for a template that is unique to the category you want to upload, first looking for a filename that includes its slug, and then continuing with its ID. If that approach fails, he’ll walk away with category.php instead, and then archive.php. Your WordPress archive should include posts from all of your categories, after all, so it makes sense to include it within this particular hierarchy.
We’ve also included WordPress tags in this section, because they’re both taxonomic elements. Also, your hierarchies are exactly the same, except that it will replace all instances of category with tags. category-{slug}.php becomes tag-{slug}.phpand so on.
5. Custom Post Types
In case you’re not familiar with custom post types, they’re basically post types that don’t fall into the default WordPress classifications. For example, if you have a blog that focuses on ratings, you might want to create a custom post type called ratings and customize it to include additional features.
Creating custom post types, however, is a topic for another time. For now, suffice it to say that these content types have their own hierarchy:
- archive-{post_type}.php
- archive.php
- index.php
As you can see, this hierarchy is not as developed as some of the others. However, you still have a couple of levels of templates before you get to index.phpwhich is enough to create complex pages.
6. Search results pages
Things get a bit simpler now with these last two WordPress page types. First we have the search results, which in most cases are extremely simple when it comes to their design. That simplicity is reflected in its hierarchy:
- search.php
- index.php
In this case, WordPress simply defaults to index.php If you can’t find a custom template for your…
