WP Template Hierarchy Checklist for Classic and Block Themes

The WordPress (WP) template hierarchy is the fixed list of file names WordPress checks, from the most specific to the most general, to decide which theme file renders the current URL. It loads the first file that exists, so a single post in a theme with single.php skips singular.php and index.php entirely. If no specific file matches, everything falls back to index.php. That single rule explains most of the surprises you’ll meet when building a custom theme. Below is a checklist of theme files, context by context, followed by the reasons the wrong template loads and how to find out which file WordPress actually picked. How WordPress chooses the template file The mechanism takes three steps. First, WordPress parses the URL and runs the main query, which sets conditional tags such as is_single(), is_page() or is_category(). Then wp-includes/template-loader.php walks through those conditionals in a fixed order and builds a list of candidate file names for the first one that is true. Finally, locate_template() returns the first candidate it finds on disk, and WordPress includes it. The order of those checks matters more than most guides admit. WordPress tests for a 404 before a search, a search before the front page, and the front page before the blog index. So a front-page.php file wins over every page template for the site root. Without that file, WordPress moves on, and a static front page falls back to the page hierarchy. Child themes fit into the same loop. For each candidate name, WordPress looks in the child theme folder first, then in the parent theme, and only then moves to the next name. So a parent’s category-news.php beats a child’s category.php, which catches many people out. Related reading: Understanding the WordPress Theme Hierarchy. Checklist for the front page and singular templates Each list runs from the file WordPress tries first to its last resort. Tick the files your theme really needs; you never have to create all of them. Front page and blog posts index front-page.php, used for the site root whatever the Reading settings say home.php, when the front page shows your latest posts the page hierarchy below, when the front page is a static page index.php The blog posts index on its own (for example /blog/, set under Settings > Reading) uses home.php, then index.php. Many people expect home.php to style the homepage; in WordPress terms, “home” means the posts index. Single posts and custom post types a custom template chosen in the editor (the $custom.php slot) single-{post-type}-{slug}.php single-{post-type}.php single.php singular.php index.php Example: a product custom post type (CPT, a content type you register beyond posts and pages) gets its own layout through single-product.php. A standard blog post looks for single-post.php before single.php. Pages the page template assigned in the editor page-{slug}.php, for example page-contact.php page-{id}.php, for example page-7.php page.php singular.php index.php A custom page template is any theme file whose header contains a Template Name: comment. Since WordPress 4.7, a Template Post Type: line makes the same template available to posts and custom post types too. Attachments Media attachment pages first try MIME-based names such as image.php or video.php, then attachment.php, then the single post chain. Most custom themes can ignore them. The same instinct shows in the work of a design studio worth a look. Checklist for archives, search and 404 pages Category and tag archives category-{slug}.php or tag-{slug}.php category-{id}.php or tag-{id}.php category.php or tag.php archive.php index.php Custom taxonomy archives taxonomy-{taxonomy}-{term}.php taxonomy-{taxonomy}.php taxonomy.php archive.php index.php Post type, author and date archives 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 A post type archive only exists if register_post_type() sets has_archive to true. Without it, archive-product.php never loads, however carefully you name it. Search results and 404 Search: search.php, then index.php Not found: 404.php, then index.php Both chains are short, so a theme without these two files shows search results and errors in the generic index.php layout. We’d create both in any client theme: a helpful 404 page with a search form keeps visitors on the site. Why the wrong template loads When your edit doesn’t show up, WordPress is almost always loading a more specific file than the one you changed. These are the usual cases, written as symptom, cause and fix. You edit single.php and nothing changes. A single-post.php, a custom template assigned in the editor, or the same file in the parent theme wins first. Check the editor’s Template setting and both theme folders. page-about.php stopped working. Someone changed the page slug. Slug-based files break silently on a rename, so prefer an assigned page template for pages that clients may rename. home.php styles the blog, not the homepage. That’s by design. Use front-page.php for the site root. A new custom post type returns 404. The rewrite rules (WordPress’s URL patterns) don’t know it yet. Save Settings > Permalinks once to flush them. The live site still shows the old layout. A page cache serves stored HTML, so PHP templates don’t run for those visitors. Purge the cache before you debug the hierarchy. Debugging checklist: which file is WordPress using? Read the body classes. If the theme calls body_class(), the <body> tag lists the context: single-post, page-id-7, category-news or page-template-…. That tells you which branch of the hierarchy applies. Install Query Monitor. This free debugging plugin shows the template file in use, the full list of candidates WordPress considered, and the template parts loaded. Print the template in development. The template_include filter receives the path of the chosen file. It runs just before WordPress includes that file: add_filter( ‘template_include’, function ( $template ) { if ( current_user_can( ‘manage_options’ ) ) { error_log( ‘Template: ‘ . $template ); } return $template; } ); Always return $template. Otherwise the page renders blank. Change the candidate list if you must. Since WordPress 4.7, filters named {$type}_template_hierarchy (for example single_template_hierarchy) let you add or reorder names before WordPress searches for them. Plugins such as WooCommerce also hook into template_include, so a plugin can

WP Template Hierarchy Checklist for Classic and Block Themes Read More »