WordPress Template Hierarchy
The WordPress Template Hierarchy is the system WordPress uses to determine which theme file to use when displaying a particular page on your website. It’s like a flowchart that WordPress follows to choose the best matching template file based on the type of content being requested (e.g., post, page, archive, 404, etc.).
Why It Matters
Understanding the template hierarchy is essential for:
- Customizing how specific content types look
- Building flexible, modular themes
- Creating custom layouts for pages, posts, taxonomies, etc.
Basic Example
If you visit a single blog post, WordPress looks for templates in this order:
single-post-{slug}.php
single-post.php
single.php
singular.php
index.php
It uses the first file it finds in your theme.
Common Template Files
Template Type | Example File | Used For |
---|---|---|
Front page | front-page.php | Static homepage |
Blog index | home.php | Posts page |
Single post | single.php , single-post.php | Blog post details |
Page | page.php , page-about.php | Static pages |
Archive | archive.php | Category, tag, author, date archives |
Category archive | category.php , category-news.php | Category-specific layouts |
Custom post type | single-glossary.php | e.g., single-portfolio.php |
Search results | search.php | Search result pages |
404 error | 404.php | Not found pages |
Attachment | attachment.php , image.php | Media file pages |
Author archive | author.php | Posts by author |
Custom Templates
You can also assign custom templates to pages using:
/*
Template Name: Custom Landing Page
*/
This allows the template to be selected in the Page editor.
How WordPress Chooses Templates
- The user requests a URL (e.g.,
/category/news
) - WordPress determines the content type
- It searches for the most specific matching template
- If none found, it falls back to more general ones
- Ultimately,
index.php
is the final fallback
Developer Tip
To visualize the hierarchy flow, use the Template Hierarchy diagram — a popular reference among theme developers.