WordPress Template Hierarchy
written by: Jeff McNearWordPress Template Hierarchy
The WordPress core code contains query strings to decide which template or set of templates should be used to display the page which searches down through the template hierarchy until it finds a matching template file.
To determine which template file to use, WordPress will match every query string to a query type to decide which page is being requested (for example, a search page, a category page, etc). Core code selects the template in the order determined by the template hierarchy by looking for template files with specific names in the current theme’s directory using the first matching template file as specified by the hierarchy.
Most templates will pull in files for the sidebar, header & footer
sidebar.php
<?php get_sidebar();?>
header.php
<?php get_header(); ?>
footer.php
<?php get_footer();?>
With the main content body being handled by a hierarchy of named files:
The “preference hierarchy” by condition is as follows:
Homepage
- front-page.php
- home.php
- page.php
- index.php
Single post
- single-{post-type}.php
- single.php
- singular.php
- index.php
Single page
- page-{slug}.php
- page-{template-name}
- page.php
- singular.php
- index.php
Category archives
- category-{slug}.php
- category.php
- archive.php
- index.php
Custom Taxonomies
- taxonomy-{taxonomy}.php
- taxonomy.php
- archive.php
- index.php
Tag archives
- tag-{slug}.php
- tag.php
- archive.php
- index.php
Author archives
- author-{nicename}.php
- author.php
- archive.php
- index.php
Date-based archives
- date.php
- archive.php
- index.php
Search pages
- search.php <= results
- searchform.php <= search form
404 error pages
- 404.php
- index.php
Attachment pages
- attachment.php – The attachment pages are displayed using this default template.
- single-attachment.php – for displaying single attachment
- single.php – the single post type items are displayed using this default template
- index.php – The ultimate default