WordPress Template Hierarchy
The WordPress template hierarchy is the choice-structure WordPress uses to find out what file within the theme can be used to generate the complete, final HTML for a given web page of your WordPress web site.
Most files in a WordPress theme are actually members of the template hierarchy, and WordPress will select to call on one of them depending on what sort of data it’s attempting to show.
The template hierarchy dictates how WordPress offers with five essential web page varieties:
- There are three template files which can be more general. The fallback template file is index.php. This is used if WordPress can’t discover anything for the particular content type. It should, therefore, work correctly for archive pages, single posts and single pages (in addition to all other content varieties). A theme has to have an index.php file to work; it could, in concept, have simply this file and the stylesheet. The other basic template files are 404.php for errors and search.php for search results.
- Templates for particular archive displays, like category and taxonomy archives, allow you to show totally different content material for various listings. For instance you might use various archive template files if one post sort has custom fields you need to show, if you wish to alter the loop or if you need to add additional content material or widget areas.
- Page templates allow you to show totally different content material on totally different static pages (for instance if you wish to show a sidebar on some pages however not all). You can then select which template a given web page uses through the admin screens.
- Single post templates, together with customized post type templates, allow you to show totally different content material for various post types, for instance if you wish to use the loop differently (e.g. displaying different metadata).
- Includes, such as header.php, sidebar.php and footer.php, may be created for various areas for the location in place of or in addition to a template file, to show totally different widgets or content material in these areas of the site. They don’t simply need to be for headers, footers, and sidebars though – you can use an include anywhere you need to save writing the same piece of code into each template file. A typical example is the WordPress Loop.
Read also: 10 Best Ways To Monetize Blog Effectively
How WordPress Chooses a Template File
When WordPress opens a web page in your website it checks what content type is being displayed after which chooses the right template file accordingly.
For static pages:
- singular.php overrides index.php, and then page.php overrides both.
- page-$slug.php or page-$ID.php override page.php when a page with the proper slug or ID is being displayed
- front-page.php beats index.php and page.php when the home page is viewed.
- A customized template page, such as page-without-sidebar.php, when selected via the web page’s admin panel, beats page.php, home.php, page-$slug.php and page-$ID.php.
For individual posts and attachments:
- singlular.php overrides index.php, and single.php beats both.
- single-post.php overrides single.php when a single post is being viewed (as against an attachment or a customized post sort).
- single-$posttype.php beats single.php when displaying a single post of a given post sort.
- single-$posttype-$slug.php beats single-$posttype.php for a post with the given slug.
- attachment.php overrides single.php when an attachment is considered.
- A custom MIME type page ($mimetype.php) beats attachment.phpwhen attachments of a given MIME sort are considered. For instance, you might use image.php, video.php, or any accepted MIME sort.
- home.php beats index.php for the home page or the main weblog. If the house web page is the primary blog page, then front-page.php beats each of these.
- archive.php beats index.php when a category, tag, date, or writer itemizing is viewed.
- category.php overrides archive.php when a selected category listing is viewed. A category-$slug.php or category-$ID.phptemplate override category.php when displaying posts from the related category ID or slug.
- tag.php overrides archive.php when a tag listing is viewed. A tag-$slug.php or tag-$ID.php file override tag.php.
- author.php overrides archive.php when the list of posts by an author is viewed, which in turn is trumped by author-$ID.php and author-$nicename.php.
- archive-$posttype.php beats archive.php when listings for a given post sort are being considered.
- date.php overrides archive.php when a list of posts for a given date is viewed.
- taxonomy.php overrides archive.php when viewing a taxonomy archive. For a selected taxonomy, taxonomy-$taxonomy.php will also beat that (the place $taxonomy is the taxonomy slug), and for a term in the taxonomy, taxonomy-$taxonoomy-$term.php will beat all of these.
- search.php trumps index.php when the outcomes from a search are viewed. You would possibly expect a search to use an archive template but it doesn’t: it both uses index.php or search.php if that exists.
- 404.php beats everything else when a URL is displayed for which WordPress can discover no content material. You can use this to show some customized content for 404 pages, corresponding to an error message and search box.
- index.php is essential for the theme to work as WordPress will fall back to it when displaying content not accounted for by any of the other templates you’ve set up.
Read also: 40 Best WordPress Business Themes
WordPress Template Hierarchy Example
If your weblog is at
http://example.com/blog/ and a customer clicks on a hyperlink to a category web page corresponding to
http://example.com/blog/category/your-cat/, WordPress seems to be for a template file within the present theme’s listing that matches the category’s ID to generate the proper web page.
More specifically, WordPress follows this process:
- Looks for a template file in the current theme’s listing that matches the category’s slug. If the category slug is “unicorns,” then WordPress looks for a template file named
category-unicorns.phpis missing and the category’s ID is 4, WordPress looks for a template file named
category-4.phpis missing, WordPress will search for a generic category template file,
category.phpdoesn’t exist, WordPress will search for a generic archive template,
archive.phpcan be lacking, WordPress will fall back to the main theme template file,