Every time somebody opens a page, WordPress asks itself what kind of page it is and looks for the most specific template the theme has for it. If it doesn't find one, it tries a more general one, and so on until it reaches the last resort, index.
That order of preference is the template hierarchy. Once you know it, you stop guessing which file to edit.
The idea in one example
Somebody opens a post in the Recipes category. WordPress looks, in this order, for:
single-post-chocolate-cake(that exact post)single-post(any post)single(any single entry)singular(any entry or page)index(anything)
It uses the first one that exists. If your theme only has index, everything is drawn with it. If tomorrow you add single, posts change and the rest stays as it was.
Classic and block themes
The order is the same in both. What changes is where the templates live.
| Classic theme | Block theme | |
|---|---|---|
| Format | PHP files | HTML files with blocks |
| Folder | The theme's root | templates/ |
| Reusable parts | header.php, footer.php, sidebar.php | parts/ |
| Edited | With a code editor | In Appearance → Editor |
| Example | single.php | templates/single.html |
In block themes, changes made in the site editor are saved in the database and win over the theme's files. If you edit the file and nothing happens, that is why: reset the template from the editor.
The whole hierarchy
Read each row from left to right: WordPress goes to the next option if the previous one doesn't exist. They all end in index.
| Page | Order |
|---|---|
| Front page | front-page → (if it's a page) the page's order, or home |
| Posts page | home |
| Single post | single-post-{slug} → single-post → single → singular |
| Custom content | single-{type}-{slug} → single-{type} → single → singular |
| Page | Chosen template → page-{slug} → page-{id} → page → singular |
| Category | category-{slug} → category-{id} → category → archive |
| Tag | tag-{slug} → tag-{id} → tag → archive |
| Custom taxonomy | taxonomy-{taxonomy}-{term} → taxonomy-{taxonomy} → taxonomy → archive |
| List of a custom type | archive-{type} → archive |
| Author | author-{name} → author-{id} → author → archive |
| Date | date → archive |
| Search | search |
| Not found | 404 |
| Attachment | By file type → attachment → single → singular |
The official reference, with the drawing that everyone prints, is in the developer handbook.
Two that confuse people
home is not the home page. It is the list of posts. The page your visitors see first is front-page. In Settings → Reading you decide whether the front page shows the latest posts or a fixed page.
page and single are different. page is for pages (About, Contact); single for posts and other content. Both fall back to singular.
How to see which template is in use
- Block theme: open the page, and in the admin bar click Edit site. The editor opens the template being used.
- Classic theme: install Query Monitor. Its Template panel shows the file and all the ones WordPress looked for before.
- Without plugins: look at the classes of the
<body>element in the page's source.single-post,category-recipesorpage-id-42tell you what kind of page it is.
Common tasks
A different design for one category
Block theme: Appearance → Editor → Templates → Add template → Category, and choose the category. Classic theme: copy category.php (or archive.php) as category-recipes.php and edit it.
A different design for one page
Block theme: edit the page, open Template in the sidebar and create a new one. Classic theme: create a file with this comment at the top, and it will appear as an option:
<?php
/*
Template Name: Wide landing page
Template Post Type: page
*/
get_header();
while ( have_posts() ) {
the_post();
the_content();
}
get_footer();A useful 404 page
Edit the 404 template: one sentence, the search box and links to your main sections. It is the most forgotten template and one of the quickest to improve.
Products in WooCommerce
WooCommerce has its own templates, which follow the same logic: single-product, archive-product, taxonomy-product_cat. In a classic theme they are copied into a woocommerce folder of the child theme; in a block theme they appear in the editor.
Don't edit the parent theme
An update erases what you changed in its files. Do it in a child theme, where a file with the same name takes precedence. Our guide to child themes explains how to make one in five minutes.
The order between parent and child is worth knowing: specificity wins first. If the parent has single-post.php and the child only single.php, posts use the parent's, because it is more specific.
Parts: what repeats
Header, footer and sidebar are separate pieces that the templates call. In classic themes, with get_header() or get_template_part( 'parts/card' ); in block themes, they are template parts. If what you want to change appears on every page, it is probably in a part and not in a template. For the footer, see how to edit it.
When something doesn't fit
- You created the file and it isn't used. Check the name: plural, hyphens and slugs must match exactly. Clear the cache.
- Your archive returns 404. Open Settings → Permalinks and save without changing anything.
- A page builder ignores the template. Elementor, Divi and others can replace whole templates from their own panel.
- A plugin imposes its own. Course, event and shop plugins do it; look for their override folder in the documentation.
Try these changes on a staging copy. If you would rather have somebody build the templates, our development team takes on WordPress work like this.
Frequently asked questions
Which file is indispensable?
In a classic theme, style.css and index.php. In a block theme, style.css and templates/index.html.
Can I add levels of my own?
Yes, with the {type}_template_hierarchy filter, for example to look for templates by month or by role.