Back to Blog

WordPress Template Hierarchy Explained for Beginners

TL;DRThe WordPress template hierarchy is the set of rules WordPress uses to decide which theme file displays a given URL. It checks the most specific template first and falls back to more general ones until it finds a match, ending at index.php. A single “product” post, for example, checks single-product-{slug}.php, single-product.php, single.php, singular.php and then index.php. Block themes follow the same order but use .html files in the /templates folder. To customize specific content, add the matching template in a child theme or your own theme.

You edit a template in your theme and nothing changes on the page. Or you change one page and a dozen others change with it. Both usually come down to the WordPress template hierarchy.

people in a room using laptops

This guide shows exactly how WordPress picks a template, with the real file chains for posts, pages and archives, how block themes fit in, and how to reuse markup with template parts.

What is the WordPress template hierarchy?

Every request to a WordPress site, whether it’s a blog post, a page, a category archive or a search, gets matched to one template file in the active theme. WordPress builds a list of candidate file names from the most specific to the most general, then uses the first one that exists. If none of the specific templates exist, it falls back to index.php (or templates/index.html in a block theme), which every theme must have.

That fallback design is why a theme can be very small and still display every type of content, and why adding one specific file lets you change one type of page without touching anything else.

Single posts and custom post types

For a single post of any post type, WordPress checks these files in order. Here the post type is product and the post’s slug is blue-widget:

  1. single-product-blue-widget.php (single-{post-type}-{slug}.php)
  2. single-product.php (single-{post-type}.php)
  3. single.php
  4. singular.php
  5. index.php

For regular blog posts, the post type is post, so single-post.php is checked before single.php. If a post has a custom template assigned in the editor (a file with a Template Post Type: header), that template is used first.

Pages

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

  1. The custom page template selected in the editor, if any
  2. page-about.php (page-{slug}.php)
  3. page-42.php (page-{id}.php)
  4. page.php
  5. singular.php
  6. index.php

The front page is a special case. If a theme has front-page.php, WordPress uses it for the site’s front page whether that’s set to show latest posts or a static page. The blog posts index uses home.php, then index.php.

Archives

Archive templates follow the same specific-to-general pattern:

  • Category: category-{slug}.php → category-{id}.php → category.php → archive.php → index.php
  • Tag: tag-{slug}.php → tag-{id}.php → tag.php → archive.php → index.php
  • Custom taxonomy: taxonomy-{taxonomy}-{term}.php → taxonomy-{taxonomy}.php → taxonomy.php → archive.php → index.php
  • Custom post type archive: archive-{post-type}.php → archive.php → index.php
  • Author: author-{nicename}.php → author-{id}.php → author.php → archive.php → index.php
  • Date: date.php → archive.php → index.php

Search results use search.php, and missing pages use 404.php, each falling back to index.php. Attachments have their own chain starting with the file’s MIME type, such as image.php, then attachment.php, single-attachment-{slug}.php, single-attachment.php, single.php and singular.php.

A worked example

Say a client wants their case studies, a custom post type called case_study, to have a different layout from blog posts, and one flagship case study to have a completely custom layout.

  • Add an archive template. Create archive-case_study.php for the listing page. Blog archives still use archive.php.
  • Add a single template. Create single-case_study.php for every case study. Blog posts still use single.php.
  • Add a one-off template. Create single-case_study-flagship-project.php for the one special case study. Every other case study keeps the standard layout.

Nothing else on the site changes, because each new file only matches the requests it is named for.

Reusing markup with get_template_part()

Templates shouldn’t repeat the same markup. Put shared pieces, such as a card or a content block, in their own files and load them with get_template_part():

<?php
// archive-case_study.php
get_header();

if ( have_posts() ) :
    while ( have_posts() ) :
        the_post();
        // Loads template-parts/content-case_study.php,
        // falling back to template-parts/content.php.
        get_template_part( 'template-parts/content', get_post_type() );
    endwhile;
    the_posts_pagination();
else :
    get_template_part( 'template-parts/content', 'none' );
endif;

get_footer();

Since WordPress 5.5, get_template_part() also accepts a third argument, an array of data the part can read from $args:

get_template_part( 'template-parts/card', null, array(
    'show_excerpt' => true,
    'heading_level' => 3,
) );

Template parts follow the same child-theme logic as full templates. If a child theme contains template-parts/card.php, it is used instead of the parent’s version.

How block themes use the hierarchy

Block themes, the kind edited with the Site Editor, follow the same hierarchy but use HTML files made of block markup instead of PHP templates:

  • Templates live in the /templates folder, for example templates/single-product.html, templates/page.html, templates/archive.html and the required templates/index.html.
  • Reusable sections such as the header and footer live in /parts and are included with the Template Part block.
  • Design settings such as colors, typography and spacing live in theme.json.

There is one important difference. When someone edits a template in the Site Editor, WordPress saves the edited version in the database, and that copy takes priority over the file in the theme. If you update templates/single.html and see no change, check Appearance → Editor → Templates for a customized version, and reset it if the file should win.

Classic and block approaches can also be combined. A classic theme can add block template parts, and hybrid setups are common on sites built with ACF blocks.

Child themes and safe customization

If you’re customizing a theme you didn’t build, put new templates in a child theme. WordPress checks the child theme first for every file in the hierarchy, then the parent. That means you can override a single template, such as single.php, while the parent theme continues to receive updates.

For a custom theme your team owns and maintains, add templates directly to the theme and keep it in version control.

Troubleshooting: which template is being used?

  • Use Query Monitor. The free Query Monitor plugin shows the template file used for the current page, the template parts it loaded and the full list of candidate files WordPress considered.
  • Check for a more specific file. If your edit to single.php doesn’t appear, a single-post.php or a custom template assigned in the editor is probably taking priority.
  • Check plugin filters. Plugins such as WooCommerce load their own templates and can be overridden from a /woocommerce folder in your theme. Other plugins can change the selection through the template_include filter, which is covered in our guide to WordPress hooks.
  • Clear caches. Page caching and object caching can show stale output after a template change.

Conclusion

The template hierarchy always moves from specific to general and always ends at index.php. Once you know the chain for posts, pages and archives, you can target exactly the content you want with one new file, keep shared markup in template parts, and avoid surprises in both classic and block themes.

Need custom templates built for your site or your clients’ sites? Our WordPress development team builds custom themes in both classic and block formats. Get a proposal.

Get started now

Start with a risk-free, no obligation proposal delivered to your inbox in one business day or less.

Get Started
  • 5-Star Rated Technical Partner
  • 100% White-Label - Sign our NDA
  • 90-Day Code Guarantee
Hey Reliable logo

Welcome to Hey Reliable

We’d love to hear from you. Please use the form below or email info@heyreliable.com.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.
Secret Link