MODULE 03 · FUNCTIONALITY & CUSTOMIZATION

Understanding the Template Hierarchy

Ever wonder how WordPress decides which PHP file builds a given page? Today you'll learn the template hierarchy — the lookup order that lets you target a single post, a whole category, or the entire site with one well-named file.

Day 23 of 30 Intermediate ~20 min

One Request, Many Possible Templates

Every time someone visits a page on a WordPress site, WordPress has to decide which PHP file should generate that page's HTML. It does this by checking a specific, predictable order of filenames inside the active theme — the first matching file it finds wins. This system is the template hierarchy.

Understanding this order means you can create a file with a very specific name and know exactly which pages it will control, without touching anything else on the site.

The Files You'll See Most Often

Themes rarely use every possible template file — but these cover the vast majority of real sites.

FILE CONTROLS
front-page.php

The site's homepage, taking priority over every other template.

single.php

An individual blog post's page.

page.php

An individual static page, like About or Contact.

archive.php

A listing page — category, tag, date, or author archives.

index.php

The universal fallback used when no more specific file exists.

THINK OF IT AS

WordPress always asks "is there a more specific file for this exact page?" before falling back to something more general.

Targeting a Single Post Type or Slug

lookup-order.txt
// Single "testimonial" post type, checked in this order:
single-testimonial.php  // most specific — just this post type
single.php              // general single-post fallback
singular.php            // fallback shared by posts and pages
index.php               // final fallback for any request

The same pattern applies to pages, archives, and categories — add the post type or slug to the filename to target it precisely.

Building a Template for One Page

  • Decide what you're targeting — a single page, a post type archive, or a specific category.
  • Copy an existing template, like page.php, into your child theme as a starting point.
  • Rename the file to match the hierarchy pattern, such as page-contact.php for the Contact page.
  • Edit the copied file to change layout or content specific to that page.
  • Save it in your child theme's root folder, not the parent theme.
  • Visit the page to confirm the new template is being used.
  • How Precise Should You Get?

    ONE FILE PER PAGE Creating a uniquely named template for every single page quickly becomes dozens of nearly identical files to maintain.
    TARGET ONLY WHAT'S DIFFERENT Reserve slug-specific templates for pages that truly need a different layout — everything else can share page.php.

    Common Template Hierarchy Mistakes

    WATCH FOR THESE
    WRONG FILENAME PATTERN A typo in the hyphenated slug or post type name means WordPress silently skips the file entirely.
    FORGETTING CACHING A caching plugin can keep serving the old template's output after a new file is added — clear the cache to check.
    BLOCK THEMES USE templates FOLDER Full Site Editing themes store equivalent files inside a templates/ folder using theme.json and template parts.

    Breaking Down What You Just Learned

    1

    A fixed lookup orderWordPress checks filenames from most specific to most general for every request.

    2

    index.php is the ultimate fallbackEvery theme must include it, since nothing else is guaranteed to exist.

    3

    Filenames encode targetingAdding a slug or post type name, like page-contact.php, narrows exactly what a file controls.

    4

    Child theme files take priorityA template in the child theme is used before the same-named file in the parent.

    5

    Only override what's differentReserve targeted templates for pages that genuinely need a unique layout.

    Try It Yourself

    EXERCISE

    Duplicate page.php in your child theme as page-about.php, add a visible change like a background color, and confirm only the About page shows the new style.

    Build a Targeted Template

    HANDS-ON EXERCISE

    Giving one page of your capstone site a unique layout

    By the end of today, one page on your capstone site should be running its own dedicated template file.

    1. Pick a page that would benefit from a different layout than the rest of the site.
    2. Copy the relevant base template into your child theme.
    3. Rename it following the template hierarchy naming pattern.
    4. Make one visible change to confirm the new file is loading.
    5. Check that no other pages were affected by the change.

    Recap

    TEMPLATE HIERARCHY

    The fixed order WordPress uses to pick which file builds a given page.

    index.php

    The universal fallback template every theme is required to have.

    SLUG-SPECIFIC FILE

    A template named for one exact page, post type, or category.

    CHILD THEME PRIORITY

    Templates in the child theme override matching files in the parent.

    Key Takeaways

    • WordPress selects a page's template using a fixed, predictable lookup order.
    • Every theme needs an index.php as the final fallback for any request.
    • Filenames like page-contact.php or single-testimonial.php target one specific page or post type.
    • Templates placed in a child theme always take priority over the same file in the parent.
    • Reserve targeted templates for pages that truly need a different layout, not every page.
    Course Overview