← Back to all tutorials

Template File Hierarchy

Understand how WordPress decides which template file to use — the template hierarchy and fallback system.

Template File Hierarchy

When someone visits a URL on your WordPress site, WordPress follows a template hierarchy — a set of rules that determines which PHP template file to use. Understanding this hierarchy is essential for building themes.

How the Hierarchy Works

WordPress looks for the most specific template file first, then falls back to more general ones:

  1. Check for the most specific template for the current page type
  2. If it doesn't exist, try the next one in the hierarchy
  3. If nothing matches, fall back to index.php

Common Template Hierarchy Chains

Page TypeWordPress Looks For (In Order)
Homepagefront-page.phphome.phpindex.php
Single Postsingle-{post-type}.phpsingle.phpsingular.phpindex.php
Pagepage-{slug}.phppage-{id}.phppage.phpsingular.phpindex.php
Categorycategory-{slug}.phpcategory-{id}.phpcategory.phparchive.phpindex.php
Tagtag-{slug}.phptag.phparchive.phpindex.php
Authorauthor-{nicename}.phpauthor.phparchive.phpindex.php
Searchsearch.phpindex.php
404404.phpindex.php

Visual Example

A visitor goes to /category/javascript/. WordPress looks for:

  1. category-javascript.php — specific to the "javascript" category slug ❌ Not found
  2. category-5.php — specific to category ID 5 ❌ Not found
  3. category.php — generic category template ❌ Not found
  4. archive.php — generic archive template ✅ Found! → Uses this

Homepage Templates

The homepage has two settings (Settings → Reading):

SettingTemplate Used
"Your latest posts"front-page.phphome.phpindex.php
"A static page"front-page.php → selected page template → page.php

front-page.php always wins for the homepage, regardless of the reading settings.

Template Files We'll Create

FileWhat It Handles
front-page.phpThe homepage (hero + featured content)
page.phpStatic pages (About, Contact)
single.phpIndividual blog posts
archive.phpCategory, tag, and date archives
search.phpSearch results
404.phpPage not found

Conditional Tags

WordPress also provides conditional tags to check the current page type:

<?php if (is_front_page()) : ?>
    <!-- Homepage-specific content -->
<?php elseif (is_single()) : ?>
    <!-- Single post content -->
<?php elseif (is_page()) : ?>
    <!-- Page content -->
<?php endif; ?>

Key Takeaways

  • WordPress uses a template hierarchy to decide which template file to load
  • It goes from most specific to most general, always falling back to index.php
  • front-page.php always controls the homepage display
  • Archive templates handle categories, tags, dates, and authors
  • Understanding the hierarchy helps you create the right template for each page type