WordPress templates themes are quite popular among both technical and nontechnical users. However, if you wish to develop a theme all by yourself you would need to understand more about how it functions and the first thing is to understand the WordPress theme hierarchy. A theme is a vast term that includes multiple elements including but not limited to images, style sheets, JavaScript and templates. The streamlined combination and display of these elements determine the final look of your WP site.
The benefits of learning about WordPress template hierarchy
As opposed to common notion you don’t have to be a genius for creating your WordPress theme. However, it is important to get yourself acquainted with the hierarchy of WordPress. To put in short a single template file or combination of different template files are employed to create the pages of a WordPress website. These template files are fetched from a specific area of the WP web page.
The particular areas of your website are affected by the templates with specific filenames and help you in identifying the template names used for specific item type.
Hence if you want to go deeper into the WordPress and wish to develop your theme the very first thing is to get acquainted with the WordPress template hierarchy.
For the different pages, WordPress uses a specific naming pattern. Following is the hierarchy level in the appropriate order that will help you understand the same more clearly.
Homepage
By default the homepage shows your site’s latest blog post but you can also modify it by going to the reading page in the settings page (in you WP admin)
· front-page.php – The WordPress will search this file if you use static front page and will employ it for showing the front page
· Home.php – for the users having a static front page but don’t have front-page.php template in the theme, the WordPress will find a home.php file. It also employs it to show blog posts on your home page
· index.php – In case both the front-page.php and home.php are not available, the WordPress falls back to index.php, WP’s default fallback template for displaying any of the site’s pages.
Single post
WP goes through these files for showing the single post
· single-{post-type}-{slug}.php – for showing/altering a specific individual post in any post type. Example post type: ‘features’, post slug: Apple-phone, WordPress would find single-features-Apple-phone.php.
· single-{post-type}.php – WordPress will then search for the template to display this specific type of post. For example, post type: features, then WordPress would look for single-features.php.
· Single.php – Next, WordPress will go to single.php.
· Singular.php – This is the best substitute for a single item display from any post type.
· Index.php –As the last fallback the WordPress will go to index.php.
Single page Template Files
WordPress pages are one of the default post types. They allow you to create static pages in your website instead of posts. See our guide on the difference between posts vs. pages.
· Custom Page Template – This is the page that has been assigned to a specific page.
· page-{slug}.php – For the page slug reach-us, WordPress will use page-reach-us.php.
· page-{id}.php – For the page ID 19, the WordPress will find template file page-19.php.
· Page.php – This template is used for displaying all the static pages.
· Singular.php – All single post type ultimately fall back to this file as the default substitute
· Index.php – As mentioned this is the ultimate default option.
Template files for Category archives
The category related page is showed using these templates:
· category-{slug}.php – For displaying category archive page for a particular category. For category slug features, WordPress will find the category-features.php template.
· category-{id}.php – Next the WordPress will search the templates with its ID. For the category ID 19, WordPress will search category-19.php.
· Category.php – This is used as the default template to display all category archive pages.
· Archive.php – The default template that WordPress uses to display the archive pages.
· Index.php – The ultimate template.
Template files for tag archives
The tag archive pages are displayed using these files
· tag-{slug}.php – For the tag’s slug phones, WordPress will find tag-phones.php.
· tag-{id}.php – For the tag’s ID 19, the WordPress will find tag-19.php template.
· Tag.php – The default template for tag archives
· Archive.php – This is the default template for any type archive page.
· Index.php – The ultimate substitute
Template files for custom taxonomy archives
Categories and tags are two default WordPress taxonomies. Users can create their custom taxonomies as well. Here is how WordPress looks for templates to display custom taxonomy pages.
WordPress uses categories and tags as the default WordPress taxonomies but it is possible for you to create your taxonomy depending upon your specific requirements. However, here is how the WordPress searches the templates for displaying the taxonomy pages:
· taxonomy-{taxonomy}-{term}.php – For the custom taxonomy niche, containing the term ‘technology’, WordPress will search taxonomy-niches-technology.php.
· taxonomy-{taxonomy}.php – For the taxonomy niche, the WordPress would search taxonomy-niche.php.
· Taxonomy.php – This is the default template for showing any custom taxonomy archives.
· Archive.php – This is the default template for all the WP’s archive pages
· index.php– The ultimate template
Template files for custom post types
This is the way the WordPress searches templates for displaying custom post type archives.
· archive-{post type}.php – If your post type is features, WordPress will search archive-features.php.
· Archive.php – This is the default template used to display the archive pages in WordPress.
· Index.php – The ultimate template of WP.
Template files for displaying author archives
For each WP author (of your site) the WordPress generate the archive pages. We are going to explain the path used by WP to search author archive pages:
· author-{nichename}.php – For an author named Tom author-Tom.php.
· author-{id}.php – If the user ID is 9, the WordPress will search author-9.php.
· Author.php – This is the default template that displays the WordPress author archive pages.
· Archive.php – This is the default template for displaying all the archive pages.
· Index.php – The default template
Template files for date-based archives
If you want WordPress to display your posts based on a date based archive pages (specific months and years), then here is the way it does the same.
· Date.php – This is the default template for the date-based archives.
· Archive.php – This is the default template that displays author archive pages (in WP).
· Index.php – The default ultimate template of WordPress.
Template files for search pages
· search.php – This is the default page used to show the search results in WordPress
· Searchform.php – The template for displaying search forms
· Index.php – The ultimate template in WordPress.
Template files for 404 error pages
If the WordPress cannot look for the requested content it will show the page 404 error
· 404.php – Te 404 page is displayed using these default templates
· Index.php – The ultimate default template in WordPress.
Template files for attachment pages
· MIME_type.php – Put simply Mime Type in the file type and examples include image.php, video.php, application.php, pdf.php, etc.
· 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
Template files for embeds
You can also render an embedded post in the WordPress Since WordPress 4.5
· embed-{post-type}-{post format}.php – WordPress will search the post type and post format template. For the feature with video, WordPress will search embed-features-video.php.
· embed-{post-type}.php – For the post type features, WordPress would find embed-features.php.
· embed.php – The ultimate substitute for all templates. .
Conclusion
WordPress Template Hierarchy plays a vital role while learning the WordPress development. However its complexity can overwhelm a new learner, especially the ones who are from non-programming background. The best way to learn WordPress template hierarchy is to simplify the complex things and start learning in small phases.