LESSON 03 / 04

The Template Hierarchy

WordPress looks for the most specific template for each page and falls back step by step to index.php. Learn the order and you control every page.

๐ŸŽ“Mediumlevel
โฑ๏ธ35 minto finish
โœ๏ธ1activities

๐Ÿ“‚ How Themes Work๐Ÿท๏ธ WordPress

๐ŸŽฏ By the end of this lesson you canโ€ฆ

  • Predict which template WordPress uses
  • Create specific templates (single, page, archive, 404)
  • Use the most specific file only when needed
Common lookups
Visitor seesWordPress tries (in order)
A single postsingle-{post_type}-{slug}.php โ†’ single-{post_type}.php โ†’ single.php โ†’ singular.php โ†’ index.php
A page “about”page-about.php โ†’ page-{id}.php โ†’ page.php โ†’ singular.php โ†’ index.php
Category “news”category-news.php โ†’ category.php โ†’ archive.php โ†’ index.php
Search resultssearch.php โ†’ index.php
Not found404.php โ†’ index.php
Home pagefront-page.php โ†’ home.php (blog) or page.php โ†’ index.php

The lookup itself is simple logic โ€” write it:

๐Ÿ› ๏ธ Pick the template

Write pickTemplate($candidates, $files): given the ordered list of candidate names and the files your theme has, return the first candidate that exists (or index.php).

๐Ÿ“Œ Key takeaways

  • Most specific wins: single-product.php โ†’ single.php โ†’ singular.php โ†’ index.php.
  • Pages: page-about.php โ†’ page.php; categories: category-news.php โ†’ category.php โ†’ archive.php.
  • Start with index.php; add specific templates only when a page needs to look different.

Finished reading & practising?

Saved in this browser. Create a free account to keep it forever.