1. Template Hierarchy
- Definition: Template hierarchy is WordPress’s built-in system for determining which template file to use for displaying a particular type of content (post, page, category, archive, etc.).
- How it works: WordPress follows a logical order of preference. It starts from the most specific template and falls back to more general ones if the specific file doesn’t exist.
- Example:
For a page with ID 42, WordPress will look for templates in this order:page-42.php(most specific)page-{slug}.php(e.g.,page-about.php)page.phpsingular.phpindex.php(fallback)
- Use case: You don’t need to manually assign these; WordPress automatically uses the hierarchy to decide the template.
2. Page Templates
- Definition: Page templates are custom templates that a developer can create and assign to a specific page from the WordPress admin dashboard.
- How it works:
- You create a PHP file in your theme folder and add a comment at the top:
<?php /* Template Name: Custom About Page */ ?> - Then, in the WordPress admin under Page Attributes → Template, you can select this custom template.
- You create a PHP file in your theme folder and add a comment at the top:
- Use case: You want a page to look or behave differently than the default
page.phpor follow the hierarchy.
Key Differences
| Feature | Template Hierarchy | Page Templates |
|---|---|---|
| Control | Automatic, built-in system | Manual assignment by user/developer |
| Specificity | Determines templates based on content type & slug | Explicitly created for specific pages |
| Flexibility | Limited to WordPress naming rules | Fully customizable |
| Usage | WordPress decides which file to load | Admin selects which template to apply |
| Example | page-42.php, single-post.php, archive.php | custom-about-page.php with Template Name |
✅ Summary:
- Template hierarchy: Automatic system WordPress uses to find the most suitable template file.
- Page templates: Custom templates created by developers for specific pages, manually selected from admin.
