WordPress Template Hierarchy
The Template Hierarchy in WordPress is a system that determines which template files are used to display content on your site. WordPress uses a set of rules to choose the appropriate template based on the type of content being requested.
Understanding the template hierarchy is essential for customizing your WordPress theme, as it allows you to control how different pages, posts, categories, archives, and other content types are displayed. The hierarchy follows a specific order, where WordPress looks for a template file that matches the current request, from most specific to most general.
Basic Structure
Section titled “Basic Structure”When WordPress needs to render a page, it follows a set order to find the correct template. Below is an overview of the typical template files WordPress will look for, starting with the most specific:
01. Custom Templates (Custom Post Types, etc.)
Section titled “01. Custom Templates (Custom Post Types, etc.)”WordPress first looks for a custom template for specific content types. For example, if you are viewing a custom post type like “movie,” WordPress will look for:
single-movie.php(for individual movie posts)archive-movie.php(for movie archive pages)
02. Single Post Templates
Section titled “02. Single Post Templates”If no custom template is found, WordPress will then search for the template for a single post:
single.php(for individual posts)
03. Page Templates
Section titled “03. Page Templates”WordPress will then check for a template for a static page:
page.php(for individual static pages)
04. Category Templates
Section titled “04. Category Templates”If you’re viewing a category archive, WordPress will look for:
category-{slug}.php(for a category with a specific slug)category-{ID}.php(for a category with a specific ID)category.php(fallback for category pages)
05. Tag Templates
Section titled “05. Tag Templates”If you’re viewing a tag archive, WordPress will look for:
tag-{slug}.php(for a specific tag)tag-{ID}.php(for a specific tag ID)tag.php(fallback for tag pages)
06. Date-based Archive Template
Section titled “06. Date-based Archive Template”For archive pages based on dates (like by year or month), WordPress will search for:
date.php(for date-based archives)
07. Author Template
Section titled “07. Author Template”If you’re viewing content by a specific author, WordPress looks for:
author-{ID}.php(for a specific author’s archive)author.php(fallback for author pages)
08. Search Template
Section titled “08. Search Template”If you’re viewing a search results page, WordPress looks for:
search.php(for search results pages)
09. 404 Template
Section titled “09. 404 Template”If WordPress can’t find a specific template or content for the request, it will use the 404 template:
404.php(for not-found pages)
Hierarchy for Common Templates
Section titled “Hierarchy for Common Templates”Here’s a simplified version of how WordPress checks templates for various content types:
-
Single Post:
single-{post-type}.php→single.php→index.php -
Page:
page-{slug}.php→page-{ID}.php→page.php→index.php -
Category:
category-{slug}.php→category-{ID}.php→category.php→index.php -
Tag:
tag-{slug}.php→tag-{ID}.php→tag.php→index.php -
Custom Post Type:
single-{custom-post-type}.php→single.php→index.php
Knowledge Check
Test your understanding of this section
Loading questions...