HomeWORDPRESSWhat is the difference between template hierarchy and page templates?

What is the difference between template hierarchy and page templates?

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:
    1. page-42.php (most specific)
    2. page-{slug}.php (e.g., page-about.php)
    3. page.php
    4. singular.php
    5. index.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.
  • Use case: You want a page to look or behave differently than the default page.php or follow the hierarchy.

Key Differences

FeatureTemplate HierarchyPage Templates
ControlAutomatic, built-in systemManual assignment by user/developer
SpecificityDetermines templates based on content type & slugExplicitly created for specific pages
FlexibilityLimited to WordPress naming rulesFully customizable
UsageWordPress decides which file to loadAdmin selects which template to apply
Examplepage-42.php, single-post.php, archive.phpcustom-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.

Share: 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *