What is the WordPress Template Hierarchy? Priorities by Page Type
Last updated:
When WordPress generates a page, it determines which template file to call based on the template hierarchy. I will explain what a template hierarchy is with an example and introduce the template hierarchy for each type of page.
What is Template Hierarchy?
A template hierarchy is a system (rule) that WordPress uses to decide which template files to use when generating a page.
Each page type has a priority of which template file to call.
Look for the corresponding template file in the theme directory by file name.
How the template hierarchy works
The template file to be invoked is determined by the following process.
- Determine the type of page from the requested URL
- Find the template file at the top of the template hierarchy for each page type.
- Use the first matching template file
- If it doesn’t match, look for the next file in the hierarchy
- If none of them match, use index.php
Example of a template hierarchy
Let’s look at the flow of how the template file is selected when the category page is actually displayed.
- Category pages are requested
- If the category slug is test, use the template file category-test.php if you have one.
- If the category ID is 7, use the template file category-7.php if available.
- If you have a template file called category.php, use it
- Use a template file called archive.php if you have one
- Using index.php
Why do we need a template hierarchy?
If you have a WordPress template hierarchy, you don’t need to prepare a page for each URL.
If you have the same page structure, you can shorten your development time by calling the same template file.
When you create a theme, understanding the template hierarchy will help you to edit the appropriate template.
Template Hierarchy by Page Type
The following is the priority order of template files for each type of page to be displayed. They are arranged in order of priority.
Home Page
By default, WordPress is set up to display the latest posts on your homepage. This page is also called the Blog Post Index.
If “Your latest posts” is selected in the “Your homepage displays” section, it will have the following priority.
- front-page.php
- home.php
- index.php
Front Page
If “Reading Settings” is selected in the “A static page” section, the priority is different depending on whether or not you have designated a Posts page for the Homepage.
Homepage
If “Homepage” is selected, the following priorities are in order.
- front-page.php
- Custom Template File
- page.php
- singular.php
- index.php
If you select a template for a given fixed page other than the “Default template“, the custom template file you select will be preferred.
Posts page only
If the homepage is not selected and only the “Posts page” is selected, the following priorities will be given.
- front-page.php
- home.php
- singular.php
- index.php
Privacy Page
The order of priority depends on whether or not you have selected “Privacy Policy” as your privacy policy page in the “Privacy Settings” section.
Privacy Policy
If “Privacy Policy” is selected, the following priorities will apply.
{slug} is the slug of the page and {id} is the ID of the page.
- privacy-policy.php
- Custom Template File
- page-{slug}.php
- page-{id}.php
- page.php
- singular.php
- index.php
If privacy-policy.php does not exist, if you select a fixed page template other than “default template” in the specified fixed page template, the custom template file you selected will take precedence.
Except for Privacy Policy
If anything, other than “Privacy Policy” is selected, the following priorities will apply.
{slug} is the slug of the page and {id} is the ID of the page.
- Custom Template File
- page-{slug}.php
- page-{id}.php
- page.php
- singular.php
- index.php
Single Post Page
The single post pages will have the following priority.
{post-type} is the post type and {slug} is the slug of the post page.
- single-{post-type}-{slug}.php
- single-{post-type}.php
- single.php
- singular.php
- index.php
Single Page
Single pages are the following priorities.
{slug} is the slug of the page and {id} is the ID of the page.
- Custom Template File
- page-{slug}.php
- page-{id}.php
- page.php
- singular.php
- index.php
Category Page
Category pages are in the following order of priority.
{slug} is the slug of the category and {id} is the ID of the category.
- category-{slug}.php
- category-{id}.php
- category.php
- archive.php
- index.php
Tag Page
Tag pages are in the following order of priority.
{slug} is the slug of the tag and {id} is the tag ID.
- tag-{slug}.php
- tag-{id}.php
- tag.php
- archive.php
- index.php
Author Page
The author page is in the following order of priority.
{name} is the author’s user name and {id} is the author’s ID.
- author-{name}.php
- author-{id}.php
- author.php
- archive.php
- index.php
Date Page
The date pages are in the following order of priority.
- date.php
- archive.php
- index.php
Search Result page
The search results page will be in the following priority order.
- search.php
- index.php
404 (Not Found)Page
404 (Not Found) pages have the following priority.
- 404.php
- index.php
Attachment Page
Attachment pages are in the following order of priority.
{MIME-type} is for image/video/pdf, etc. {slug} is the slug of the attachment.
- {MIME-type}.php
- attachment.php
- single-attachment-{slug}.php
- single-attachment.php
- single.php
- singular.php
- index.php
Priority when {MIME-type} is text-plain.
- text-plain.php
- plain.php
- text.php
Embeds Page
Embed pages are prioritized in the following order
{post-type} is the post type and {post-format} is the post format.
- embed-{post-type}-{post-format}.php
- embed-{post-type}.php
- embed.php
- wp-includes/theme-compat/embed.php
Custom Post Type Page
Custom post type pages will have the following priority.
{post-type} is filled with the post type.
- archive-{post-type}.php
- archive.php
- index.php
Custom Taxonomy Pages
Custom taxonomy pages have the following priority.
{taxonomy} is a taxonomy and {term} is a term.
- taxonomy-{taxonomy}-{term}.php
- taxonomy-{taxonomy}.php
- taxonomy.php
- archive.php
- index.php