As discussed, template files are standard and reusable files, which are used to create web pages on your WordPress site. Some form files (such as the form header and footer) are used on all pages of your site, while others are used only under specific conditions.
This article explains how WordPress determines which template file(s) to use on individual pages. If you want to customize a pre-existing WordPress theme, it will help you decide which template file to edit.
WordPress uses the query string to specify which template or set of Theme to use to display the page. The query string is the information in the link for each part of your website. It comes after the initial question mark and may contain a number of parameters separated by ampersands.
- Simply put, WordPress searches through the template hierarchy until it finds a matching template file. To specify the template file to use, WordPress:
- matching each query string with a query type to select the desired page (for example, a search page, a category page, etc.);
Selects the template in the order specified by the template hierarchy;
Finds form files with given names in the current template directory and uses the first matching form file as defined by the hierarchy.
Except for the core index.php template file , you can choose whether or not to execute a specific template file.
If WordPress cannot find a template file with a matching name, it will move to the next file in the hierarchy. If WordPress cannot find any matching template file, the theme’s index.php file will be used.
examples # examples
If your blog is at http://example.com/blog/ and the visitor clicks a link to a taxonomy page like http://example.com/blog/category/your-cat/ , then WordPress looks for a sample file in the current template directory that matches Category ID to create the correct page. More specifically, WordPress follows this procedure:
- Finds a template file in the current template directory that matches the class slug. If the slug category is “unicorns”, then WordPress looks for a template file named category-unicorns.php .
- If category-unicorns.php is missing and category ID is 4, WordPress looks for a template file named category-4.php .
- If category-4.php is missing, then WordPress will look for a generic category template file, category.php.
- If category.php is not present , WordPress will search for a generic archive template, archive.php.
- If the archive.php file is also missing, then WordPress will return to the main theme template file, index.php.
Visual overview # Visual overview
The following diagram shows the template files called to create a WordPress page based on the WordPress theme hierarchy.
Template hierarchy in detail # Template hierarchy in detail
While the template hierarchy is easy to understand as a schema, the following sections describe the order in which template files are called by WordPress for a number of query types.
Note: Under each title there is a set of options, only one can be used to display the file you want on your site.. We have given all the options, just to show how calling template files with WordPress.
Show home page # Show home page
front page display #
default article #
custom template file
# custom categories
# custom post types
view author #
search result #
404 Not Found) #