WordPress

The WordPress template hierarchy explained

Avi, the Avenux Techspire mascot, illustrating: The WordPress template hierarchy explained

You make a change in single.php, refresh the page, and nothing happens. Or you create page-about.php and suddenly your About page looks different without you doing anything else. Both of these come down to one thing: the template hierarchy.

The idea is simple. For every request, WordPress works out what kind of page it is, then looks for template files in a fixed order, from the most specific to the most general. It uses the first one it finds. If it finds nothing else, it falls back to index.php, which is why every classic theme must have that file.

Single posts

When someone opens a single blog post with the slug hello-world, WordPress checks for these files in order:

  1. A custom template assigned to that post in the editor (if your theme offers one)
  2. single-post-hello-world.php
  3. single-post.php
  4. single.php
  5. singular.php
  6. index.php

The pattern is single-{post-type}-{slug}.php, then single-{post-type}.php. That matters a lot for custom post types. If you register a post type called project, single-project.php will be used for every project, while regular posts keep using single.php.

Pages

Pages have their own chain. For a page with the slug about and ID 42:

  1. The page template chosen in the editor’s Template setting
  2. page-about.php
  3. page-42.php
  4. page.php
  5. singular.php
  6. index.php

Prefer the slug version over the ID version. IDs change when you move content from a local site to the live server, but slugs usually do not.

Custom page templates

If you want a template that editors can pick from a dropdown, add a header comment to any PHP file in your theme:

<?php
/*
Template Name: Full Width
Template Post Type: page, post
*/
get_header();
?>

It will now appear in the Template option for pages (and posts, because of the second line). This is the cleanest way to offer layout choices without creating a file for every page.

The home page and the blog page

This is the part that confuses almost everyone, because it depends on Settings, Reading in the admin.

  • front-page.php is used for the site’s front page, no matter what the Reading setting is. If it exists, it wins.
  • home.php is used for the blog posts index. If the front page shows your latest posts, home.php is used there (when there is no front-page.php). If you set a static front page and a separate Posts page, home.php is used on that Posts page.

So home.php does not mean “home page”. It means “the list of blog posts”. Once you remember that, the rest falls into place. If neither file exists, WordPress falls back to the usual page chain or index.php.

Archives

Archive pages list several posts grouped by something. Each type has its own chain, but they all end at archive.php and then index.php.

Category

category-{slug}.php, then category-{id}.php, then category.php, then archive.php, then index.php.

Tag

tag-{slug}.php, then tag-{id}.php, then tag.php, then archive.php, then index.php.

Custom taxonomy

taxonomy-{taxonomy}-{term}.php, then taxonomy-{taxonomy}.php, then taxonomy.php, then archive.php, then index.php.

Custom post type archive

archive-{post-type}.php, then archive.php, then index.php. This only applies if the post type was registered with has_archive set to true.

Author and date

Authors use author-{nicename}.php, author-{id}.php, then author.php. Date archives use date.php. Both fall back to archive.php and index.php.

Search and 404

Search results use search.php, then index.php. A page that does not exist uses 404.php, then index.php. Always build a proper 404.php. The fallback to index.php often shows a confusing “Nothing found” message inside a full blog layout.

Attachments

Media attachment pages have their own chain based on the file type, ending with attachment.php, single.php and singular.php. Many sites turn these pages off or redirect them, since they rarely add value.

How to see which template is being used

When a page is not doing what you expect, the first job is to find out which file is actually loading. A few ways:

  • Install the Query Monitor plugin. It shows the template file and the full list of files WordPress checked.
  • Look at the classes on the body tag in your browser’s inspector. body_class() adds things like single-post, page-template-full-width or category-news.
  • Temporarily drop a comment like <!– single.php –> at the top of each template and view the page source.

Practical tips

  • Do not create a file for every possible slug. Start with the general templates and only add specific ones when a page genuinely needs a different layout.
  • Use singular.php if your posts and pages share the same layout. It saves you from keeping two nearly identical files.
  • When a specific template only differs in one section, consider a template part loaded conditionally instead of duplicating the whole file.
  • If you are working with a child theme, a template in the child theme overrides the same file in the parent. The hierarchy is checked in the child first.

Block themes follow the same rules

If you work with block themes, the same hierarchy applies. The only difference is that templates are HTML files in the templates folder, such as templates/single.html and templates/archive.html, and they can be edited in the Site Editor. Learn the order once and it serves you in both kinds of theme.