WordPress Template Hierarchy

The WordPress Template Hierarchy is the system WordPress uses to determine which theme file to use when displaying a particular page on your website. It’s like a flowchart that WordPress follows to choose the best matching template file based on the type of content being requested (e.g., post, page, archive, 404, etc.).

Why It Matters

Understanding the template hierarchy is essential for:

  • Customizing how specific content types look
  • Building flexible, modular themes
  • Creating custom layouts for pages, posts, taxonomies, etc.

Basic Example

If you visit a single blog post, WordPress looks for templates in this order:

  1. single-post-{slug}.php
  2. single-post.php
  3. single.php
  4. singular.php
  5. index.php

It uses the first file it finds in your theme.

Common Template Files

Template TypeExample FileUsed For
Front pagefront-page.phpStatic homepage
Blog indexhome.phpPosts page
Single postsingle.php, single-post.phpBlog post details
Pagepage.php, page-about.phpStatic pages
Archivearchive.phpCategory, tag, author, date archives
Category archivecategory.php, category-news.phpCategory-specific layouts
Custom post typesingle-glossary.phpe.g., single-portfolio.php
Search resultssearch.phpSearch result pages
404 error404.phpNot found pages
Attachmentattachment.php, image.phpMedia file pages
Author archiveauthor.phpPosts by author

Custom Templates

You can also assign custom templates to pages using:

/*
Template Name: Custom Landing Page
*/

This allows the template to be selected in the Page editor.

How WordPress Chooses Templates

  1. The user requests a URL (e.g., /category/news)
  2. WordPress determines the content type
  3. It searches for the most specific matching template
  4. If none found, it falls back to more general ones
  5. Ultimately, index.php is the final fallback

Developer Tip

To visualize the hierarchy flow, use the Template Hierarchy diagram — a popular reference among theme developers.