WordPress Template Hierarchy

The Complete Developer's Guide

What Is the WordPress Template Hierarchy?

The WordPress template hierarchy is a systematic approach that determines which theme template file WordPress uses to display any given page on your website. Rather than having a single file control every page, WordPress themes consist of multiple template files, each responsible for rendering specific types of content. When a visitor requests a page, WordPress follows a predetermined decision tree, checking for increasingly general template files until it finds one that exists.

This elegant fallback system means you only need to create template files for the specific page types you want to customize. For example, when displaying a single blog post, WordPress first checks for a template specifically for that post (such as single-{slug}.php). If not found, it checks for a template for that specific post type, then falls back to single.php, then to singular.php, and finally to index.php as the ultimate fallback. This cascading approach gives theme developers precise control over how different content types appear while ensuring even the most minimal theme can display any content.

Understanding this hierarchy transforms what might seem like WordPress magic into a predictable, logical process. Whether you're creating a custom theme from scratch, modifying an existing theme through a child theme, or troubleshooting why a page looks a certain way, the template hierarchy provides the roadmap you need. For developers working on professional WordPress development projects, mastering this system is foundational to building maintainable, scalable websites.

Core Template Files and How They Work

Every WordPress theme centers around a collection of template files, with index.php serving as the ultimate fallback that WordPress will use if no more specific template is found. While a theoretically functional theme could consist of only index.php, practically speaking, themes contain multiple template files that control different aspects of your site's appearance.

The header.php file contains the HTML document head, site navigation, and branding elements. WordPress loads this template on every page using the get_header() template tag, meaning any changes made to header.php automatically appear across your entire site. Similarly, footer.php contains the closing HTML elements, copyright notices, and scripts that should appear at the bottom of every page. The get_footer() template tag loads this file, providing a central place to manage site-wide footer content.

The sidebar.php template, loaded via get_sidebar(), typically contains widget areas and secondary navigation. Modern themes often use multiple sidebar templates (sidebar-{location}.php) to provide different configurations for various page types. Together, these template parts create a modular architecture where you maintain consistency while customizing individual page layouts.

Beyond these shared template parts, themes contain page-specific templates. The singular.php file serves as a shared template for both single posts and individual pages, introduced in WordPress 4.3 to reduce code duplication. The archive.php template handles display of archive pages showing multiple posts, such as category or tag archives. The search.php template controls how search results appear, while 404.php provides a custom error page for visitors who reach non-existent pages.

Key Template Files Every Developer Should Know

index.php

The universal fallback template used when no more specific template exists. Every theme requires this file.

singular.php

Shared template for both single posts and individual pages, reducing code duplication between post and page templates.

archive.php

Handles archive pages showing multiple posts grouped by category, tag, author, or date.

get_template_part()

The most powerful template tag for building modular, maintainable WordPress themes with reusable template parts.

The Front Page Hierarchy

The front page of your WordPress site represents a unique case in the template hierarchy because WordPress provides two fundamentally different ways to configure it: as your latest posts or as a static page. When you visit your site's front page, WordPress first checks whether you've configured a static front page through Settings > Reading in the WordPress admin.

If you've designated a specific page as your front page, WordPress looks for front-page.php first. This template gives you complete control over how your site's landing page appears, allowing you to create a custom design completely different from your blog listing. If your theme lacks front-page.php but you've configured a static front page, WordPress will look for a custom page template assigned to that front page before falling back to the general page.php template.

If you haven't configured a static front page, WordPress assumes you want to display your latest blog posts. In this scenario, the template hierarchy looks for home.php after skipping over front-page.php. The home.php template is specifically designed to display multiple posts in a listing format. Only if home.php is missing will WordPress fall back to the general index.php template.

Front Page Fallback Chain: front-page.php → custom page template → page-{slug}.phppage.phpsingular.phpindex.php

Homepage (Latest Posts) Fallback Chain: home.phpindex.php

Single Post and Page Templates

Individual content items in WordPress--single posts, pages, and custom post type entries--follow a detailed template hierarchy that starts with the most specific possible template and works toward increasingly general fallbacks. For a standard blog post, WordPress first checks for a template named single-{post_type}-{slug}.php, where post_type is typically "post" and slug is the post's URL-friendly identifier. This allows you to create entirely custom layouts for specific posts.

If that file doesn't exist, WordPress looks for single-{post_type}.php, which would be single.php for standard posts. Only if neither exists does WordPress use the generic single.php template. The singular.php file, introduced as a unified template for both single posts and individual pages, serves as a fallback that applies to any singular content without its own dedicated template.

Pages follow a similar but slightly different pattern. When displaying a single page, WordPress first checks for any custom page template you've assigned through the page editor's template selector. If no custom template is assigned, WordPress looks for page-{slug}.php, followed by page-{id}.php. Only then does WordPress fall back to page.php, singular.php, or index.php.

Custom page templates are PHP files with a special comment header that makes them appear in WordPress's template selector. Adding Template name: My Custom Template at the top of your PHP file creates a template that site administrators can assign to any page through the WordPress admin. When extending WordPress functionality through plugins as well, understanding how templates integrate with the broader WordPress plugin development ecosystem ensures cohesive site architecture.

Archive and Taxonomy Templates

Archive pages in WordPress display multiple posts grouped by a common characteristic--whether that's the category they belong to, the author who wrote them, the date they were published, or a custom taxonomy term they've been assigned to. Each archive type follows its own template hierarchy.

Category archives demonstrate this pattern clearly: WordPress first looks for category-{slug}.php, where slug is the category's URL-friendly name. A template for the "tutorials" category would be category-tutorials.php. If that doesn't exist, WordPress checks for category-{id}.php, where ID is the category's numeric database identifier. Failing both, WordPress falls back to category.php, then archive.php, and finally index.php.

Tag archives follow the exact same pattern with tag-{slug}.php, tag-{id}.php, tag.php, and archive.php as the fallback chain. Custom taxonomies use taxonomy-{taxonomy}-{term}.php as their most specific template, followed by taxonomy-{taxonomy}.php, then taxonomy.php.

Author archives display posts by a specific author and use author-{nicename}.php as their most specific template (where nicename is the author's user login converted to a URL-friendly format), followed by author.php, archive.php, and index.php. Date-based archives use date.php as their dedicated template for displaying posts from specific time periods like months or years. Properly structured content taxonomies not only improve user experience but also support effective SEO strategies by creating logical content groupings that search engines can easily understand.

Category Archives

Templates: `category-{slug}.php` → `category-{id}.php` → `category.php` → `archive.php` for category-specific content groupings.

Tag Archives

Templates: `tag-{slug}.php` → `tag-{id}.php` → `tag.php` → `archive.php` for displaying posts with specific tags.

Custom Taxonomies

Templates: `taxonomy-{taxonomy}-{term}.php` → `taxonomy-{taxonomy}.php` → `taxonomy.php` for custom content organization.

Author Archives

Templates: `author-{nicename}.php` → `author.php` → `archive.php` for displaying posts by specific authors.

Date Archives

Template: `date.php` for displaying posts from specific time periods like months or years.

Generic Archives

Template: `archive.php` as the universal fallback for any archive type without a more specific template.

Template Tags and the Modular System

Template tags are PHP functions that WordPress provides for outputting content and including template files within your theme. They form the connective tissue that holds the template system together, enabling you to build modular themes where common elements can be reused across multiple templates.

The get_header() function includes the header.php template file at the top of your templates, while get_footer() includes the footer.php file at the bottom. These functions accept an optional parameter telling WordPress which variant to include--calling get_header('alternative') looks for header-alternative.php instead of the default header.php. This allows themes to have different header designs for different page types.

The get_sidebar() function works similarly, including sidebar.php. Modern themes often use multiple sidebar templates to provide different configurations--perhaps a full-width template uses get_sidebar('none') to indicate no sidebar, while a blog template uses get_sidebar('blog') for a widgetized sidebar.

The get_template_part() function represents the most powerful template tag for building maintainable WordPress themes. When you call get_template_part('content', 'post'), WordPress looks for content-post.php and falls back to content.php if that specific file doesn't exist. This pattern allows you to create reusable components like post previews or product cards that can be shared across templates while still allowing for type-specific variations. A call to get_template_part('template-parts/content', 'video') first looks for template-parts/content-video.php, then falls back to template-parts/content.php. This modular approach is essential for professional WordPress development, enabling efficient code reuse and easier maintenance across complex theme projects.

Essential Template Tags

get_header()

Includes header.php at the top of templates. Use `get_header('alternative')` for header-alternative.php. Essential for site-wide consistency.

get_footer()

Includes footer.php at the bottom. Supports `get_footer('alternative')` for variant footers on different page types.

get_sidebar()

Includes sidebar.php. Modern themes use `get_sidebar('blog')` for different sidebar configurations across the site.

get_template_part()

The most powerful tag--creates reusable template parts with fallback hierarchy: `content-post.php` → `content.php`.

Block Themes and WordPress 6.x Template System

WordPress 6.x introduced Full Site Editing (FSE) and block themes, representing the most significant evolution of the template system since WordPress began. Block themes abandon traditional PHP-based template files in favor of HTML files stored in a templates/ folder at the theme root. These template files use HTML-like block markup instead of PHP code to define their structure, with content dynamically inserted by the WordPress block editor.

A block theme might include templates/index.html, templates/single.html, templates/archive.html, and templates/front-page.html, each defining structure and layout using block patterns. A single post template might define a cover block, post title block, featured image block, and post content block, with WordPress automatically populating these blocks with appropriate content for each post.

Template parts--reusable components like headers, footers, and sidebars--are stored in a parts/ folder using a similar block-based format. A parts/header.html template part might include site logo, navigation, and search blocks, while parts/footer.html includes copyright information and footer widgets.

The block template hierarchy follows similar principles but uses different file locations. WordPress first looks in the templates/ folder for templates matching the page type (single.html, archive.html, index.html), then checks the theme's patterns folder, and finally falls back to index.html. This new system integrates tightly with the WordPress block editor, allowing users to edit templates directly through the WordPress admin without touching code. Classic themes remain fully supported and widely used, especially for developers who prefer the control of PHP-based templates. For sites looking to leverage modern WordPress capabilities while maintaining optimal performance, combining block themes with AI-powered automation solutions can streamline content management workflows.

Block Theme Questions

Creating Custom Templates and Child Themes

Creating custom templates in WordPress requires understanding both the template hierarchy and safe practices that ensure modifications survive theme updates. The recommended approach for customizing an existing theme is creating a child theme, which inherits all parent functionality while allowing you to override specific templates. For a comprehensive guide to child theme creation, including best practices for file structure and template overrides, see our guide on how to create WordPress child themes.

A child theme requires only two files to function: style.css with appropriate header comments identifying it as a child theme, and functions.php that enqueues the parent's stylesheet. When WordPress loads, it checks the child theme first--any template file you place in the child theme (such as single.php or page.php) will override the parent's version while leaving the parent's files intact. This means you can customize specific templates without losing the ability to update the parent theme.

Custom page templates offer another approach for creating specialized layouts without a full child theme. Create a new PHP file with a special comment header: /* Template Name: Landing Page */. This template will appear in WordPress's template selector when editing any page, making it easy for site administrators to apply specialized layouts. This approach is particularly useful for landing pages, sales pages, or any page needing a significantly different layout.

For reusable components, creating template parts through get_template_part() provides flexibility and maintainability. Rather than copying code into multiple templates, you create a single template part file that can be included wherever needed. A call to get_template_part('template-parts/content', 'video') first looks for template-parts/content-video.php, then falls back to template-parts/content.php, keeping template files clean while enabling content-specific variations.

Custom Template Methods

Child Themes

Inherit parent functionality, override specific templates by placing files in the child theme folder. Safe from parent theme updates.

Custom Page Templates

PHP files with Template Name header that appear in WordPress's template selector dropdown for assignment to any page.

Template Parts

Reusable components via get_template_part() with fallback hierarchy for type-specific variations like content-video.php → content.php.

Block Templates

HTML-based templates in templates/ folder, editable through the WordPress site editor in WordPress 6.x and later.

Debugging and Best Practices

Understanding how to debug template issues is essential when working with the WordPress template hierarchy, as it's common to encounter situations where the wrong template seems to load or modifications don't appear to take effect. The most straightforward debugging technique is temporarily adding a visible marker to a template file--adding a comment like <!-- DEBUG: I am single.php --> near the top of a template will reveal itself in the page's HTML source, confirming which template WordPress is using.

More sophisticated debugging is available through the Query Monitor plugin, which displays detailed information about which template is loading on any given page, along with the current query, enqueued scripts and styles, and database queries. This plugin is invaluable for troubleshooting complex template hierarchies and understanding which templates WordPress is selecting. For comprehensive guidance on WordPress debugging techniques and tools, see our complete guide to WordPress debug mode and error logging.

Common issues include template files in the wrong location (must be in theme root or properly structured subdirectory), incorrect file naming (remember category-{id}.php uses numeric ID, not slug), missing Template header for custom page templates, and whitespace before PHP opening tags. Template files must be written in correct PHP format with proper opening tags and without any output before the opening tag.

Best practices for template development include keeping templates focused and modular, avoiding duplicate code by extracting common elements into template parts, and using body_class() and post_class() functions to add useful CSS classes. The body_class() function adds classes to the <body> tag describing the current page type, logged-in state, and other useful information, making it easier to style different page types through CSS.

Common Template Files Reference

Understanding the complete template hierarchy requires familiarity with all the template files WordPress recognizes and when each is used. The most fundamental templates include index.php as the universal fallback, singular.php as the combined template for single posts and pages, and archive.php as the generic template for all archive types. These three files alone can create a functional theme.

For individual content display, the hierarchy includes single.php for single posts, page.php for individual pages, and their variants: single-{post_type}.php for custom post types, page-{slug}.php for specific pages, and author.php for author archives. For content groupings, category-{slug}.php handles specific categories, tag-{slug}.php handles specific tags, taxonomy-{taxonomy}-{term}.php handles specific taxonomy terms, and date.php handles date-based archives. For specialized pages, search.php displays search results and 404.php provides the error page for missing content.

Modern block themes use an entirely different file structure based on templates/ and parts/ folders, with templates/index.html serving as the block-based equivalent of index.php. Whether working with classic PHP-based themes or modern block themes, understanding the template hierarchy provides the roadmap for controlling your theme's appearance with precision and intentionality. Professional developers working on WordPress projects benefit from understanding both approaches to choose the right tool for each project's requirements.

index.php

Universal fallback - used when no specific template exists. Every theme requires this file.

singular.php

Combined template for single posts and pages, reducing code duplication.

single.php

Single post display template for individual blog posts.

page.php

Individual page display template for WordPress pages.

archive.php

Generic archive template for all archive types.

category-{slug}.php

Specific category template by URL-friendly slug.

tag-{slug}.php

Specific tag template by URL-friendly slug.

search.php

Search results display template.

404.php

Error page template for missing content.

templates/index.html

Block theme equivalent of index.php in WordPress 6.x.

Ready to Build Custom WordPress Themes?

Understanding the template hierarchy is the foundation of professional WordPress development. Whether you're building child themes, creating custom page templates, or developing full block themes, this knowledge empowers you to control every aspect of your site's appearance.

Sources

  1. Kinsta: A Guide to the WordPress Template Hierarchy - Comprehensive guide covering fundamental concepts and visual hierarchy diagrams
  2. WPBeginner: Beginner's Guide to WordPress Template Hierarchy - Visual cheat sheets and file naming conventions
  3. Hostinger: WordPress Template Hierarchy Tutorial - Updated 2025 guide with developer examples
  4. WPShout: WordPress Template Hierarchy Explained - Block themes and WordPress 6.x updates