What is WordPress hierarchy? The way I’d explain WordPress hierarchy is a priority sequence where it decides which template file to use depending on information requested by a user.
To Illustrate this first let’s see the visual chart of it and we go through it step by step.
How to correctly view and analyze this image?
First thing we have to understand is it’s color-coding. Let’s begin.
- Anything in black box is an explanation a page type, a purpose of the corresponding WordPress template files that are connected with a line to blue and orange boxes.
- Anything in orange and blue are actual files located in a typical WordPress theme.
- Official terms are.
- Black – Page Type
- Blue – Primary Template
- Light Blue – Secondary Template
- Orange – Variable Template
So why are these labels and file names starting from opposite ends?
Let’s understand this from a user perspective. When a visitor views a WordPress site and clicks a menu link, WordPress tries to retrieve the information visitor requested in a chain of requests through it’s hierarchy.
Left, black boxed side is a “user requested click” – right orange/blue side is what template file WordPress answers with.
Why is the black box page type explanation connected with multiple files horizontally?
This is where hierarchy takes meaning. WordPress checks availability of the first file – black boxed page type is connected to. In case there is no such file in the theme directory, it checks the next file connected to its right. Last fallback template file is index.php and is used when no other template file available in a request chain. This file must be present in every WordPress theme.
Purpose of having multiple variation of the same request type is customization.
WordPress will first pick Variable Template > Secondary Template >Primary Template .
Let’s take a look at the example based on post “category”
Custom category template files give opportunity to display certain category archives in different ways.
For example category of posts can be designed by category.php and will be exclusively used by categories only.
To finish up the explanation lets get specific.
To design exclusive category archive template for the category “german cars” we’d create template file: “category-german-cars.php”
To design exclusive category archive template for all the categories of the theme, we’d create “category.php”
This is where category customization stops.
I’ll be writing more tutorials and guides in the future. Stay tuned.