Definition
The WordPress Template Hierarchy is a system that determines which PHP template file WordPress uses to display different types of content. WordPress looks for the most specific template file first, then falls back to more general templates if specific ones don't exist. This allows developers to create highly customized themes while ensuring every page has a template to display.
Use Cases & Examples
Custom Post Type Archives
When creating a custom post type called “products,” you can create archive-products.php
to specifically style the products archive page, while other post types fall back to the generic archive.php
template.
Category-Specific Styling
For a “news” category with ID 5, you could create category-news.php
or category-5.php
to give news articles a unique layout, while other categories use the default category.php or archive.php.
Single Post Customization
Create single-product.php
for custom post type “product” entries, single-post.php for regular blog posts, or even single-123.php
for a specific post with ID 123.
Page Template Hierarchy
For a contact page with slug “contact,” WordPress looks for templates in this order: page-contact.php
, page-2.php
(if ID is 2), page.php
, singular.php
, then index.php
.
Author Archives
Create author-john.php
for user “john,” author-5
.php for user ID 5, or fall back to author.php
for all author pages.
404 Error Pages
WordPress looks for 404.php
specifically for not-found pages, providing a dedicated template for error handling.
References & Resources
WordPress Developer Documentation:
Visual Resources:
WordPress Template Hierarchy Interactive Chart: Visual representation of the complete hierarchy.
Template Hierarchy Cheat Sheet: Quick reference guide.
Further Reading:
Conditional Tags in WordPress: Essential for template logic.
Template Loading Process: Understanding how WordPress selects templates.