WordPress Template Hierarchy Explained: A Comprehensive Guide
WordPress powers over 40% of the internet, and much of its flexibility stems from its ability to dynamically display content using a structured system of template files. At the core of this system lies the WordPress Template Hierarchy—a logical, priority-based framework that determines which template file the CMS uses to render different types of content (e.g., blog posts, pages, category archives, search results).
Whether you’re a theme developer crafting custom designs, a site owner tweaking a child theme, or someone learning WordPress fundamentals, understanding the template hierarchy is critical. It empowers you to:
- Design unique layouts for specific content types (e.g., a custom template for your "Products" custom post type).
- Override default templates without modifying core files (via child themes).
- Debug issues when content isn’t displaying as expected.
This guide will break down the template hierarchy in exhaustive detail, from its basic principles to advanced use cases. By the end, you’ll be able to confidently navigate, customize, and extend WordPress templates to build tailored websites.
Table of Contents#
- What Is the WordPress Template Hierarchy?
- How the Template Hierarchy Works: The Decision-Making Process
- Core Template Files: A Detailed Breakdown
- 3.1 Front Page Templates
- 3.2 Home Page (Blog Posts Index) Templates
- 3.3 Single Post Templates
- 3.4 Page Templates
- 3.5 Archive Templates
- 3.5.1 Category Archives
- 3.5.2 Tag Archives
- 3.5.3 Custom Taxonomy Archives
- 3.5.4 Date Archives
- 3.5.5 Author Archives
- 3.6 Search Results Template
- 3.7 404 Error Template
- 3.8 The Fallback: index.php
- Custom Post Types (CPTs) and the Hierarchy
- Template Parts and Includes: Modular Design
- Conditional Tags: Complementing the Hierarchy
- Overriding Templates in Child Themes
- Debugging the Template Hierarchy
- Advanced Tips and Best Practices
- Conclusion
- References
What Is the WordPress Template Hierarchy?#
The WordPress Template Hierarchy is a system that maps user requests (e.g., "show me the 'About Us' page" or "display posts in the 'Travel' category") to specific template files in your theme. It operates on a priority-based fallback system: WordPress first looks for the most specific template file for the requested content. If that file doesn’t exist, it checks for the next most specific template, and so on, until it reaches index.php—the universal fallback.
Think of it as a decision tree: For any given request, WordPress follows a predefined path of template files, prioritizing specificity. This allows theme developers to create highly customized layouts for different content types without rewriting core WordPress code.
For example, if a user visits a single blog post titled "My Summer Vacation," WordPress will:
- Look for
single-post-my-summer-vacation.php(most specific: post type + slug). - If not found, check
single-post.php(post type-specific). - If still not found, use
single.php(generic single post template). - If
single.phpis missing, fall back toindex.php.
How the Template Hierarchy Works: The Decision-Making Process#
When a user requests a page, WordPress follows these steps to select a template:
Step 1: Parse the Request#
WordPress first interprets the URL to determine the content type being requested. For example:
yoursite.com/about→ A "page" with the slug "about".yoursite.com/category/travel→ A "category archive" for "travel".yoursite.com/2023/10→ A "date archive" for October 2023.
Step 2: Check for Specific Templates#
Using the content type, WordPress references its internal hierarchy rules to find the most specific template file. It checks files in a predefined order, starting with the highest priority (most specific) and moving to lower priority (more generic).
Step 3: Fall Back to index.php#
If no specific templates exist for the request, WordPress defaults to index.php, which acts as the catch-all template for all content types.
Core Template Files: A Detailed Breakdown#
To master the hierarchy, you need to understand the key template files and their priorities. Below is a breakdown of the most common content types and their associated template hierarchies.
3.1 Front Page Templates#
The "front page" is the first page visitors see when they visit your site (e.g., yoursite.com). Its template depends on whether you’ve set a static front page or a blog posts index in Settings → Reading.
Hierarchy Order (Highest to Lowest Priority):#
front-page.php– Highest priority. Used for the front page, regardless of whether it’s a static page or blog posts index.home.php– Used iffront-page.phpdoesn’t exist and the front page is set to display the blog posts index.index.php– Fallback if neitherfront-page.phpnorhome.phpexists.
Key Note: The difference between front-page.php and home.php is a common source of confusion. Here’s the distinction:
- Use
front-page.phpto control the design of your site’s front page, regardless of whether it’s a static page or the blog posts index. - Use
home.phponly if your front page is set to display the blog posts index (via Settings → Reading → Your homepage displays → Your latest posts). If you set a static page as your front page,home.phpwill instead control the blog posts index (e.g.,yoursite.com/blog).
3.2 Home Page (Blog Posts Index) Templates#
The "home page" (or blog posts index) displays a list of your latest blog posts. It is distinct from the front page if you’ve set a static front page (via Settings → Reading → Your homepage displays → A static page → Posts page).
Hierarchy Order:#
home.php– Highest priority for the blog posts index.index.php– Fallback ifhome.phpdoesn’t exist.
Example: If you set your static front page to yoursite.com (using front-page.php) and your blog posts index to yoursite.com/blog, WordPress will use home.php to render yoursite.com/blog.
3.3 Single Post Templates#
Single post templates control the layout of individual posts (e.g., yoursite.com/2023/10/my-summer-vacation). This includes posts of the default post type and custom post types (CPTs).
Hierarchy Order (for default post type):#
single-{post-type}-{slug}.php– Most specific: Targets a single post by its post type and slug. Example:single-post-my-summer-vacation.phpfor a post with slugmy-summer-vacationand typepost.single-{post-type}.php– Targets all posts of a specific post type. Example:single-review.phpfor a custom post typereview.single.php– Generic template for all single posts (default fallback for theposttype).index.php– Final fallback.
Example Workflow: For a custom post type book with the slug the-great-gatsby:
- WordPress first checks for
single-book-the-great-gatsby.php. - If missing, it looks for
single-book.php. - If still missing, it uses
single.php. - If
single.phpis missing, it falls back toindex.php.
3.4 Page Templates#
Pages are static content types (e.g., "About Us," "Contact") and have one of the most flexible hierarchies, allowing for page-specific, ID-specific, or template-specific designs.
Hierarchy Order:#
page-{slug}.php– Targets a page by its slug (the URL-friendly version of the page title). Example:page-about.phpfor a page with slugabout.page-{ID}.php– Targets a page by its database ID. Example:page-42.phpfor a page with ID 42 (find a page’s ID by editing it and checking the URL:post=42).page-{template-slug}.php– Targets pages assigned a custom page template. Custom templates are defined by adding a comment header (see Advanced Tips). Example: If you create a template namedFull Width Pagewith the slugfull-width, the file would bepage-full-width.php.page.php– Generic template for all pages (default fallback).index.php– Final fallback.
Pro Tip: Custom page templates let you create unique layouts (e.g., "Landing Page" or "Portfolio Gallery") and assign them to specific pages via the Page Attributes meta box in the WordPress editor.
3.5 Archive Templates#
Archives display collections of posts, such as posts in a category, tagged with a keyword, or published in a specific month. WordPress supports several archive types, each with its own hierarchy.
3.5.1 Category Archives#
Displays posts in a specific category (e.g., yoursite.com/category/travel).
Hierarchy Order:
category-{slug}.php– Targets a category by its slug. Example:category-travel.phpfor the "Travel" category.category-{ID}.php– Targets a category by its database ID. Example:category-5.phpfor a category with ID 5.category.php– Generic template for all category archives.archive.php– Fallback for all archive types (ifcategory.phpis missing).index.php– Final fallback.
3.5.2 Tag Archives#
Displays posts tagged with a specific keyword (e.g., yoursite.com/tag/sunset).
Hierarchy Order:
tag-{slug}.php– Targets a tag by its slug. Example:tag-sunset.php.tag-{ID}.php– Targets a tag by its ID. Example:tag-12.php.tag.php– Generic template for all tag archives.archive.php– Fallback.index.php– Final fallback.
3.5.3 Custom Taxonomy Archives#
Custom taxonomies (e.g., "Genre" for a book custom post type) have their own archive hierarchy.
Hierarchy Order:
taxonomy-{taxonomy}-{term-slug}.php– Most specific: Targets a term within a custom taxonomy. Example:taxonomy-genre-fiction.phpfor the "Fiction" term in the "Genre" taxonomy.taxonomy-{taxonomy}.php– Targets all terms in a custom taxonomy. Example:taxonomy-genre.phpfor the "Genre" taxonomy.taxonomy.php– Generic template for all custom taxonomy archives.archive.php– Fallback.index.php– Final fallback.
3.5.4 Date Archives#
Displays posts published in a specific date range (e.g., yoursite.com/2023/10 for October 2023).
Hierarchy Order:
date.php– Only template for date archives (no specific date-slug or ID variants).archive.php– Fallback ifdate.phpdoesn’t exist.index.php– Final fallback.
3.5.5 Author Archives#
Displays posts written by a specific author (e.g., yoursite.com/author/john-doe).
Hierarchy Order:
author-{nicename}.php– Targets an author by their "nicename" (URL-friendly username). Example:author-john-doe.php.author-{ID}.php– Targets an author by their user ID. Example:author-1.phpfor the admin user (ID 1).author.php– Generic template for all author archives.archive.php– Fallback.index.php– Final fallback.
3.6 Search Results Template#
Displays results for a search query (e.g., yoursite.com/?s=vacation).
Hierarchy Order:#
search.php– Only template for search results.index.php– Fallback ifsearch.phpdoesn’t exist.
3.7 404 Error Template#
Displays when a user requests a page that doesn’t exist (e.g., yoursite.com/nonexistent-page).
Hierarchy Order:#
404.php– Only template for 404 errors.index.php– Fallback if404.phpdoesn’t exist (though this is rare; most themes include404.php).
3.8 The Fallback: index.php#
index.php is the universal fallback template for all content types. If no other template file exists for a request, WordPress will use index.php. Think of it as the "emergency template"—it ensures your site never breaks, even if critical templates are missing.
Well-designed themes keep index.php minimal, as it’s rarely used if other templates are in place. It typically includes calls to get_header(), get_footer(), and a loop to display content.
4. Custom Post Types (CPTs) and the Hierarchy#
Custom Post Types (CPTs) extend WordPress beyond default posts and pages (e.g., product, event, or portfolio). They integrate seamlessly with the template hierarchy, with their own specific templates.
Single CPT Templates#
To customize the layout of individual CPT entries (e.g., a single product post), use the following hierarchy:
single-{post-type}-{slug}.php– Most specific: Targets a single CPT entry by post type and slug. Example:single-product-winter-jacket.php.single-{post-type}.php– Targets all entries of a specific CPT. Example:single-product.phpfor allproductposts.single.php– Fallback (if no CPT-specific template exists).index.php– Final fallback.
CPT Archive Templates#
To customize the archive page for a CPT (e.g., yoursite.com/products), use:
archive-{post-type}.php– Highest priority for CPT archives. Example:archive-product.php.archive.php– Fallback ifarchive-{post-type}.phpdoesn’t exist.index.php– Final fallback.
Note: CPT archives are disabled by default. To enable them, set 'has_archive' => true when registering your CPT (via register_post_type()).
5. Template Parts and Includes: Modular Design#
WordPress encourages modular theme design through template parts—reusable components like headers, footers, and post content blocks. These are not part of the hierarchy itself but work alongside it to keep templates DRY (Don’t Repeat Yourself).
Common template parts include:
header.php– Site header (loaded viaget_header()).footer.php– Site footer (loaded viaget_footer()).sidebar.php– Sidebar (loaded viaget_sidebar()).content.php– Post content (loaded viaget_template_part('content')).
For example, single.php might include:
<?php get_header(); ?>
<main>
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part('content', 'single'); // Loads content-single.php ?>
<?php endwhile; ?>
</main>
<?php get_sidebar(); ?>
<?php get_footer(); ?>Here, content-single.php contains the HTML for displaying a single post’s content, keeping single.php clean and focused on layout.
6. Conditional Tags: Complementing the Hierarchy#
While the hierarchy controls which template file is loaded, conditional tags let you modify content within a template based on the request type. For example, you might want to display a banner on single posts but not on pages.
Common conditional tags include:
is_front_page()– True if the current page is the front page.is_home()– True if the current page is the blog posts index.is_single()– True if viewing a single post (including CPTs).is_page()– True if viewing a static page.is_category()– True if viewing a category archive.is_search()– True if viewing search results.
Example: Add a custom message to single posts in single.php:
<?php if (is_single()) : ?>
<div class="post-banner">Thanks for reading! Share this post below.</div>
<?php endif; ?>Conditional tags are powerful when you want to make minor tweaks to a template without creating a new specific template file.
7. Overriding Templates in Child Themes#
WordPress child themes let you modify parent theme templates without losing changes when the parent theme updates. To override a template, simply copy the parent theme’s template file into your child theme directory and edit it there.
How It Works: WordPress prioritizes child theme files over parent theme files. For example, if your parent theme has single.php, and your child theme also has single.php, WordPress will use the child theme’s single.php.
Steps to Override a Template:
- Create a child theme (if you haven’t already).
- Copy the template file from the parent theme to the child theme directory (e.g.,
parent-theme/single.php→child-theme/single.php). - Edit the child theme’s template file to make your changes.
Best Practice: Only override templates you need to modify. This keeps your child theme lightweight and reduces maintenance.
8. Debugging the Template Hierarchy#
If your template isn’t displaying as expected, use these tools to debug:
8.1 Template Hierarchy Visual Chart#
The official WordPress Template Hierarchy Chart is a visual reference for which template files are used for different requests.
8.2 Display the Current Template#
Add this code to your theme’s functions.php to show which template file is being used (useful for debugging):
function show_current_template() {
if (current_user_can('administrator')) {
global $template;
echo '<!-- Current Template: ' . basename($template) . ' -->';
}
}
add_action('wp_footer', 'show_current_template');This prints a comment in your site’s HTML (viewable via "View Page Source") showing the active template (e.g., <!-- Current Template: single.php -->).
8.3 Check File Names and Locations#
- Ensure template files are in your theme’s root directory (or child theme directory).
- Verify file names are case-sensitive (Linux servers) or case-insensitive (Windows servers). For consistency, use lowercase filenames (e.g.,
single.php, notSingle.php). - Avoid typos (e.g.,
singel.phpinstead ofsingle.php).
9. Advanced Tips and Best Practices#
9.1 Use Custom Page Templates#
Create reusable page templates by adding a comment header to a PHP file. For example, page-fullwidth.php:
<?php
/*
Template Name: Full Width Page
Template Post Type: page
*/
get_header(); ?>
<main class="full-width">
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
</main>
<?php get_footer(); ?>This template will appear in the Page Attributes → Template dropdown when editing a page.
9.2 Modify the Hierarchy with Hooks#
Use the template_include filter to programmatically override the template hierarchy. For example, force all "Travel" category posts to use category-travel-special.php:
function custom_category_template($template) {
if (is_category('travel')) {
$new_template = locate_template(array('category-travel-special.php'));
if (!empty($new_template)) {
return $new_template;
}
}
return $template;
}
add_filter('template_include', 'custom_category_template');9.3 Keep Templates Focused#
Each template should handle one content type (e.g., single.php for single posts, page.php for pages). Avoid overloading templates with conditional logic for unrelated content types.
9.4 Test Across Content Types#
Always test custom templates across different content types to ensure fallbacks work. For example, if you create single-product.php, verify that regular posts still render correctly with single.php.
10. Conclusion#
The WordPress Template Hierarchy is the backbone of theme development, enabling precise control over how content is displayed. By mastering its priority-based fallback system, you can create highly customized, modular themes that adapt to any content type—from blog posts and pages to custom post types and archives.
Whether you’re building a simple blog or a complex e-commerce site, understanding the hierarchy empowers you to:
- Design targeted layouts for specific content.
- Maintain clean, DRY code with template parts.
- Safely customize themes via child themes.
- Debug issues with confidence.
With practice, the hierarchy will become second nature, allowing you to build flexible, professional WordPress sites.
11. References#
- WordPress Template Hierarchy Documentation – Official WordPress Developer Handbook.
- WordPress Template Tags – Functions like
get_header()andthe_post(). - Child Themes Guide – WordPress Developer Handbook.
- Custom Post Types – WordPress Developer Handbook.
- Conditional Tags – WordPress Developer Handbook.
- Template Hierarchy Visual Chart – Interactive tool by Misha Rudrastyh.