WordPress Template Hierarchy Explained for Beginners
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.
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:
single-product-blue-widget.php(single-{post-type}-{slug}.php)single-product.php(single-{post-type}.php)single.phpsingular.phpindex.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:
- The custom page template selected in the editor, if any
page-about.php(page-{slug}.php)page-42.php(page-{id}.php)page.phpsingular.phpindex.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.phpfor the listing page. Blog archives still usearchive.php. - Add a single template. Create
single-case_study.phpfor every case study. Blog posts still usesingle.php. - Add a one-off template. Create
single-case_study-flagship-project.phpfor 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
/templatesfolder, for exampletemplates/single-product.html,templates/page.html,templates/archive.htmland the requiredtemplates/index.html. - Reusable sections such as the header and footer live in
/partsand 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.phpdoesn’t appear, asingle-post.phpor 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
/woocommercefolder in your theme. Other plugins can change the selection through thetemplate_includefilter, 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