What Is The WordPress Template Hierarchy Based On? (Best solution)

WordPress follows an order called a query string that decides which template or set of templates build any of the classic seven pages mentioned above. That’s the WordPress ‘template hierarchy’ in action.

Contents

What is the WordPress template hierarchy?

WordPress will look for files in a theme to use to render the current page in a specific order. This is called a ‘template hierarchy’, that is used to create and extend WordPress themes. WordPress themes decide how your content should be displayed on your website.

What do you understand by template hierarchy?

The Template File Hierarchy #

  • Matches every query string to a query type to decide which page is being requested (for example, a search page, a category page, etc);
  • Selects the template in the order determined by the template hierarchy;

What is the default template in WordPress?

Example: header. php is a default template used in most WordPress themes. It defines the header area of web pages generated by WordPress. The header file will typically be loaded on every page of your WordPress site, allowing changes to be made to a single file, that will apply across the entire website.

What does categories mean on WordPress?

Category is one of the default taxonomies in WordPress. You can use categories to sort and group your blog posts into different sections. For example, a news website might have categories for their articles filed under News, Opinion, Weather, and Sports.

What is WordPress taxonomy?

A taxonomy within WordPress is a way of grouping posts together based on a select number of relationships. By default, a standard post will have two taxonomy types called Categories and Tags which are a handy way of ensuring related content on your website is easy for visitors to find.

How does a template help in creating a document?

A template is a document type that creates a copy of itself when you open it. For example, a business plan is a common document that is written in Word. Instead of creating the structure of the business plan from scratch, you can use a template with predefined page layout, fonts, margins, and styles.

How do I create a custom template in WordPress?

Go to WordPress Admin Panel > Pages > Add New. You can see the new custom page template listed on the right side. Create a new page and set its template to PageWithoutSidebar. Once done, Publish it.

Is front page template WordPress?

On the site front page, WordPress will always use the front-page. php does not exist, WordPress will determine which template file to use, depending on the user configuration of Settings > Reading >Front page displays, as follows: A static page: WordPress uses the Static Page template hierarchy: Custom Page Template.

How do you add a template to WordPress?

Method 1: Use the WordPress block editor (Gutenberg)

  1. Open the template editor. Start by creating a new post using the block editor.
  2. Design your template using Theme blocks. You are now in the template editor, where you can start designing the layout of your posts.
  3. Save your template and apply it to your posts.

What is the template name for default fallback?

5. index. php – The default fallback template.

How do I change the default template in WordPress?

Changing a Page Template

  1. Go to Dashboard > Pages > All Pages.
  2. Go to one of the existing pages that you want to modify and select Quick Edit.
  3. Go to the Templates options (1), choose one of the available templates and click on Update(2). Once it’s done, click on the Apply button (3).

What are WordPress template parts?

Template parts are incomplete pieces of WordPress PHP templates that get pulled out into their own PHP file. Creating a template part is easy, you first start out by creating a new PHP file. For example, we can create a file called template-example. php, you can type the code that you want to reuse.

What is WordPress template?

Templates are the files which control how your WordPress site will be displayed on the Web. These files draw information from your WordPress MySQL database and generate the HTML code which is sent to the web browser. More information on creating Themes can be found at Theme Development.

Where is the default template in WordPress?

On your WordPress Dashboard, choose Appearance→Editor to look at the template files within a theme. To view and edit a template file, click the template name in the list on the right side of the page.

Template Hierarchy

In the last section, we explored how template files are modular, reusable files that are used to produce web pages for your WordPress site. Template files are used on all of your site’s pages, while others (such as header and footer templates) are only utilized under specified circumstances. It is explained in this article how WordPress decides which template file(s) to utilize on which specific pages. If you wish to make changes to an existing WordPress theme, this will assist you in determining which template files need to be changed.

The Template File HierarchyThe Template File Hierarchy

WordPress makes use of the query string to determine which template or group of templates should be used to show the page on the website. It is the information contained in the link to each section of your website that is referred to as the query string. To put it another way, WordPress crawls through the template hierarchy until it finds a template file that matches the criteria. WordPress uses the following criteria to choose which template file to use:

  1. Checks each query string against a query type to determine which page is being asked (for example, a search page or an archive of all pages on a website). Selects a template from the template hierarchy in the order indicated by the hierarchy
  2. In the current theme’s directory, it looks for template files with certain names, and it uses the first matching template file, according to the hierarchy.

Checks each query string against a query type to determine which page is being requested (for example, a search page or an archive of all pages on a website); Specifies the order in which the templates should be selected based on the template hierarchy. In the current theme’s directory, it looks for template files with certain names, and it uses the first matching template file, according to the hierarchy;

ExamplesExamples

For example, whenever a reader to your blog visits a category page by clicking on a link such as, WordPress looks for a template file in the current theme’s directory that matches the category’s ID in order to build the appropriate page. WordPress, to be more explicit, uses the following procedure:

  1. This function looks for a template file in the current theme’s directory that has the same slug as the category being searched for. WordPress looks for the category-unicorns.php template file if the category’s ID is 4
  2. If the category-unicorns.php template file is missing and the category’s ID is 4, WordPress looks for the category-4.php template file
  3. If the category-4.php template file is missing, WordPress will look for a generic category template file,category.php
  4. If the archive template archive.php is also missing, WordPress will fall back to the default template,default.php
  5. If

Visual OverviewVisual Overview

The following graphic illustrates which template files are invoked in order to build a WordPress page based on the WordPress template hierarchy structure. This diagram can also be interacted with in some way. a b c d

The Template Hierarchy In DetailThe Template Hierarchy In Detail

While a graphic of the template hierarchy is more intuitive to grasp, the following sections illustrate the sequence in which WordPress calls template files for a variety of query types. a b c d

Home Page displayHome Page display

It is WordPress’s default setting to display your most recent blog entries on the site’s home page. The blog posts index is the name given to this page. You may also choose to have your blog articles appear on a static page distinct from your website. It is necessary to utilize the template filehome.php to generate the blog posts index, regardless of whether it is used as the front page or as a separate static page. If the file home.php does not exist, WordPress will fall back to index.php. Please keep in mind that if the front-page.php template exists, it will take precedence over the home.php template.

Front Page displayFront Page display

The front-page.phptemplate file is used to generate the front page of your website, regardless of whether the front page shows the blog posts index (as indicated above) or a static page. The front page template (front.php) takes priority over the blog posts index (home.php) template in the database.

Depending on the settings in Settings Reading, if the front-page.phpfile does not exist, WordPress will either use thehome.phporpage.phpfiles, or it will use thefront-page.phpfile. If neither of those files exists, the index.php file will be used instead.

  1. In the front page displayssection of Settings Reading, the PHP file front-page.php is used for both ” your latest posts ” and ” a static page.” If WordPress cannot locate front-page.php and ” your latest posts” is set in the front page displayssection, it will seek for home.php. Additionally, WordPress will look for this file when the posts page is set in the front page displayssection
  2. Page.php– when ” front page ” is set in the front page displayssection
  3. Index.php– when ” your latest posts ” is set in the front page displayssection but home.php does not exist
  4. Index.php– when ” your latest posts ” is set in the front page displayssection but home.php does not exist Alternatively, when the front page is specified but page.php does not exist

As you can see, there are several rules that govern the course that WordPress travels. Using the chart above, you can figure out what WordPress will show the most effectively. a b c d

Privacy Policy Page displayPrivacy Policy Page display

When your site’s Privacy Policy page is shown, theprivacy-policy.phptemplate file is utilized to generate it. The Privacy Policy page template takes priority over the static page (page.php) template in terms of functionality. If the privacy-policy.php file does not exist, WordPress will fall back to either thepage.php or thesingular.php files, depending on which templates are currently accessible. If neither of those files exists, the index.php file will be used instead.

  1. In Settings Privacy, the Privacy Policy page may be customized with a custom template file called privacy-policy.php. The custom template file is the page template that was given to the Privacy Policy page in the Change your Privacy Policy page section of Settings Privacy. Seeget page templates()
  2. Page-.php– If the page slug is privacy, WordPress will hunt for a template that contains the word privacy. page-privacy.php
  3. Page-.php– If the page ID is 6, WordPress will seek for a file called page-privacy.php or page-.php. page-6.php
  4. Page.php
  5. Singular.php
  6. Index.php
  7. Page-6.php

Single PostSingle Post

The single post template file is used to create a single post that may be shared with others. WordPress takes the following path to reach its database:

  1. Single- -.php– is a PHP script that allows you to create a single page (Since 4.4) WordPress begins by searching for a template that will work for the specific content. For example, if the post type isproduct and the post slug isdmc-12, WordPress would look forsingle-product-dmc-12.php
  2. Single-.php– If the post type isproduct, WordPress would look forsingle-product.php
  3. Single.php– If the post type isproduct, WordPress would look forsingle-product.php
  4. Single.php– – WordPress then reverts to single.php and singular.php– WordPress then reverts to singular.php and index.php– WordPress then reverts to single.php and index.php – Finally, as previously said, WordPress eventually falls back to the index.php file.
You might be interested:  How To Add Woocommerce To Wordpress? (TOP 5 Tips)

Single PageSingle Page

When a static page is rendered, the template file is utilized to generate it (pagepost-type). It should be noted that, in contrast to other post kinds, page is unique to WordPress and utilizes the following path:

  1. This is the custom template file that has been allocated to the page. Seeget page templates()
  2. Page-.php– If the page slug is recent-news, WordPress will look to usepage-recent-news.php
  3. Page-.php– If the page ID is 6, WordPress will look to usepage-6.php
  4. Page.php
  5. Singular.php
  6. Index.php
  7. Seeget page templates()

CategoryCategory

Creating category archive index pages in WordPress is accomplished through the usage of the following path:

  1. Category-.php– If the category’s slug is news, WordPress will seek for the file category-news.php
  2. Otherwise, WordPress will look for the file category-.php. If the category ID is 6, WordPress will seek for the file category-6.php
  3. Otherwise, it will look for category-.php. category.php
  4. Archive.php
  5. Index.php

TagTag

WordPress utilizes the following route to show a tag archive index page: tagarchiveindex.php.

  1. Tag-.php– If the tag’s slug issometag, WordPress will look fortag-sometag.php
  2. Tag-.php– If the tag’s ID is6, WordPress will look fortag-6.php
  3. Tag.php
  4. Archive.php
  5. Index.php
  6. Tag-.php– If the tag’s slug issometag, WordPress will look fortag-sometag.php
  7. Tag

Custom TaxonomiesCustom Taxonomies

Custom taxonomies have a slightly different template file path than standard taxonomies.

  1. When a taxonomy has the name sometax and the taxonomy’s term is someterm, WordPress will seek for the file taxonomy-sometax-someterm.php. Taxonomy-sometax-someterm.php Taxonomy-post format (i.e., taxonomy-post format) is used to classify post formats. The phrases are “post-format-,” which stands for “post format.” -post-format -link.php taxonomy-.php is the link post format to use. – Suppose the taxonomy was sometax
  2. WordPress would seek fortaxonomy-sometax.php
  3. Taxonomy.php
  4. Archive.php
  5. Index.php
  6. And other related files.

Custom Post TypesCustom Post Types

In order to produce the right archive index page for Custom Post Types, the following route is used:

  1. If the post type is product, WordPress will seek for the files archive-product.php, archive.php, index.php, and archive-product.php. If the post type is not product, WordPress will look for the file archive.php.

(For information on how to render a single post type template, see the single post display section above.) a b c d Based on the above examples, it should be reasonably self-explanatory how to create author archive index pages:

  1. Author-.php– If the author’s nice name is Matt, WordPress will look forauthor-matt.php
  2. Author-.php– If the author’s ID were 6, WordPress will look forauthor-6.php
  3. Author-.php– If the author’s nice name is Matt, WordPress will look forauthor-matt.php
  4. Author-.php– If the author’s nice name is

DateDate

Archive index pages that are organized by date are shown in the following way:

Search ResultSearch Result

The same principle applies to search results as it does to other template types:

404 (Not Found)404 (Not Found)

Similarly, the following is the sequence in which the 404 template files are called:

AttachmentAttachment

In order to render an attachment page (attachmentpost-type), the following path must be followed:

  1. MIME type does not matter for.php file extensions (For example:image.php,video.php,pdf.php). For text/plain, the following path is followed (in the following order):
  1. Attachment.php
  2. Single-attachment-.php– For example, if the attachment slug isholiday, WordPress would look forsingle-attachment-holiday.php
  3. Single-attachment-holiday.php
  4. Single.php
  5. Singular.php
  6. Index.php
  7. Single-attachment

EmbedsEmbeds

The embed template file is used to display the contents of a post that has been embedded. WordPress has used the following route since version 4.5:

  1. Embedding a.php file is the first step in the WordPress process of finding a template for the specific post. For example, if the post type ispost and the audio format is present, WordPress would look forembed-post-audio.php
  2. Embed-.php– If the post type isproduct, WordPress would look forembed-product.php
  3. Embed.php– WordPress would then fall back to embed.php
  4. Embed-.php– If the post type isproduct, WordPress would look forembed- Finally, WordPress eventually falls back to its ownwp-includes/theme-compat/embed.phptemplate
  5. However, this is not recommended.

Non-ASCII Character HandlingNon-ASCII Character Handling

WordPress checks for a template for the individual article before embedding the.php file. Embed- -.php– WordPress, for example, would look forembed-post-audio.php if the post type is post and the audio format is present, and embed-.php if the post type is product; embed.php if the post type is product and the audio format is present; embed.php if the post type is post and the audio format is present, and embed.php if the audio format is present; embed-.php if the post type is product and the audio format is present; embed-.php Finally, WordPress finally falls back to its ownwp-includes/theme-compat/embed.phptemplate; however, this is not recommended.

  • Page-hello-world-.php
  • Page-hello-world- percent f0 percent 9f percent 98 percent 80.php
  • Page-6.php
  • Page.php
  • Singular.php

The same is true for post slugs, term names, and author nicenames, all of which follow the same pattern. a b c d

Filter HierarchyFilter Hierarchy

The WordPress template system has a feature that allows you to filter the hierarchy. In other words, you have the ability to insert and alter objects at specified places in a hierarchy.

_template” is the filter name used by the filter (which is contained in theget query template()function), and $type is the template type used by the filter. The following is a list of all of the filters that are currently accessible in the template hierarchy:

  • Embed template, 404 template, search template, frontpage template, home template, privacypolicy template, taxonomy template, attachment template, single template, page template, singular template, category template, tag template, author template, date template, archive template, index template

ExampleExample

Consider the default author hierarchy, which looks like this: We may change the real hierarchy by utilizing the ‘author template’ template type to place author-.php before author.php, as seen below. If author-editor.php is present in the current themes directory, a request for /author/username where username has the role of editor will be shown using author-editor.php if it is present in the current themes directory. author role template($templates = “) is a function that creates author role templates.

  1. is array($templates)!
  2. is array($templates)!
  3. when ($templates) is empty, $templates is equal to locate template(“author-$role.php”, false); else, $templates is empty.
  4. empty($new template)) array unshift($templates, $new template);return $templates; if (!
  5. Obtain the whole source code Full source code should be collapsed.

A Guide to the WordPress Template Hierarchy (2022 Edition)

We may change the real hierarchy by utilizing the ‘author template’ template type to place author-.php before author.php, as seen in the following example. Author-editor.php will be shown if author-editor.php is available in the current themes directory in response to a request for /author/username where username has the role of editor. author role template($templates = “) is a function that is used to create author roles templates. $author = get queried object();$role = $author-roles;if (! is array($templates)!

  1. is array($templates)!
  2. when ($templates) is empty, $templates is equal to locate template(“author-$role.php”, false); else, $templates is equal to else When $new template is equal to (array(“author-$role.php”)), and (!
  3. If (!
  4. adding a filter to the author template and author role template is a common practice.

Prefer to watch thevideo version?

Non-dynamic websites often render their content using static HTML and CSS files rather than dynamic HTML and CSS. WordPress, on the other hand, is a dynamic platform built on the PHP programming language. Every WordPress website loads a number of.php files, each of which governs the appearance of a certain area or component.

Each time a specific type of page is loaded, the Content Management System (CMS) searches for the template files that are appropriate for that type of page. In the case of WordPress, if you use the search tool and then view the search results page, the CMS will seek for two different template files:

  1. Search.php, which controls the appearance of your search results pages
  2. WP utilizes index.php as its default template file when it cannot locate the top option within each hierarchy, and it also uses index.php when it can’t find the top option within each hierarchy.

Your ability to access template files is dependent on the theme that you are using. Templates, stylesheets, and other elements such as pictures are all part of any WordPress theme’s set of components. Consequently, in the example above, if your theme contains the asearch.phptemplate, WordPress will automatically detect and load it. If you’re using a theme that doesn’t provide template files for the sorts of pages you wish to load, this is something to consider. The WordPress template hierarchy comes into play in this situation.

  • If WordPress cannot locate thesearch.php file, it will fall back to the next file in the hierarchy, which is index.php, in order to display the search results.
  • You should be able to create a completely working theme that uses only one template file, which in this case would be index.php.
  • Understanding the WordPress template hierarchy is one of the most critical steps you can take as a theme developer, unless that is exactly what you want.
  • ✅ This guide contains all of the information you’ll need to get started.

How the WordPress Template Hierarchy Works

As you may be aware, WordPress allows you to utilize a variety of page kinds based on what you want to publish on your site. There are seven major categories that you may choose from. These are:

  1. 404 error pages
  2. Single posts
  3. Single pages
  4. Custom post kinds
  5. Search result pages
  6. Category and tag pages
  7. Search results

Each of those pages has its own unique hierarchy, which implies that it makes use of a customized collection of template files for that particular page. Any of your theme’s directories will most likely have a collection of template files, so take a quick look around. Example of template files for a blog post listed inside the theme directory: The following is an example of template files listed within the theme directory for a blog post: WordPress theme directories include a list of template files.

Additionally, it includes a distinct template file for each page element, the majority of which should be obvious at first look, such as: Important to note is that, while each page type has its own hierarchy, they often share common template files, such as header.php and footer.php, which should be understood as follows: This implies that if you’re creating your own theme, you’ll be able to generate specific styles for each sort of page while simultaneously creating template files that you can reuse.

Create custom template files for components such as sidebars and footers that only apply to specific page types by following the steps outlined above.

When you switch themes, the variations in layouts that you notice are due to the new template files that WordPress loads into the database. Child themes also play a role in the WordPress theme hierarchy, which we’ll go over in more detail later.

Where to Locate and Change WordPress Template Files

It is possible to incorporate both PHP code and HTML markup in a single.phpfile (all of whichyou can edit). Thus, the template files for WordPress may be as complicated as you need them to be depending on your needs. Often, the template files that WordPress employs are simply a collection of functions for the parts that they are responsible for managing. Here’s a brief glance at the header.phptemplate file that the official WordPressTwenty Twenty-Onetheme makes use of, just to give you an example: The Twenty Twenty-One theme’s header template file is located in the root directory.

  1. You may view the template files included in your theme by navigating to its folder within your website’s root directory using a File Transfer Protocol (FTP) client such as FileZilla.
  2. By default, WordPress will load template files that are located either within your theme’s main directory (wp contentthemes) or within thetemplate-partsfolder of your theme’s root directory.
  3. It is possible to create subdirectories for template files that are more particular in nature, such as a full-width page or sidebar layouts.
  4. In this instance, we’re providing you with a graphic representation of the template hierarchy, courtesy of the WordPress Codex: The WordPress template hierarchy is shown in the following diagram.
  5. For the time being, let’s have a look at each page type and the template files that it makes use of.

A Breakdown of the WordPress Template Hierarchy by Page Type

Pages in WordPress are divided into seven categories. In this part, we’ll go through each of them in detail, as well as offer you with a breakdown of the template files that they utilize to create their designs. Let’s start with the first page of the book.

Front Page Template Files

The home page of your WordPress website is the initial page that most people see when they visit your website. On the other hand, in actuality, front page design might differ significantly from one site to another. WordPress, on the other hand, will seek for these three template files in the following sequence for a simple front page: If your theme doesn’t contain a front-page.phptemplate file, WordPress will default to the second option, and so on until the theme is included. As is always the case, the index.php file is the last stop on WordPress’s template decision tree before reaching the final destination.

Want to know how we increased our traffic over 1000%?

Join over 20,000 other people who receive our monthly email, which contains insider WordPress advice! Now is the time to subscribe.

Single Posts

Articles in WordPress are organized according to the single post template hierarchy. WordPress will seek for the following files in order to create the primary content of each blog post: Look at the template cheat sheet, on the other hand, and you’ll find that a more sophisticated structure arises in certain instances. This is due to the fact that WordPress allows you to specify template files for individual posts and single categories, and then defaults to single.php if either of those options cannot be found by the system.

As previously stated, each of those pieces can be represented by a separate template file.

Single Pages

Single pages are the bread and butter of most WordPress websites, following articles as the primary content. In most situations, they utilize a different template than your home page, unless both types of pages use the index.php template as their default. The following diagram depicts the single page template hierarchy: Despite the fact that the template hierarchy for single posts and pages is identical, there are a few significant variations. First and foremost, when it comes to pages, WordPress makes use of the get page templates() path for custom files, which you may use to override or redirect the page template that is used by default.

You might be interested:  Why Wordpress? (Perfect answer)

It is possible that WordPress will attempt to load each file in the correct sequence before defaulting to page.php if you have either apage–.phpor apage–phpfile on your site.

Custom Post Types

Custom post types may be used for a variety of different forms of information that you may not want to bundle with other pages or posts. Using custom post kinds, you may organize your information more effectively, and they have their own template hierarchy: The template hierarchy for custom post types is less complicated than the template structure for complete pages or posts. WordPress, on the other hand, allows you to generate template files for each custom post type, which means they don’t all have to have the same exact layouts.

Search Results Pages

We’ve already covered the template hierarchy that WordPress employs for search results pages, so let’s go through it again in a high-level manner: In general, the WordPress template hierarchy gets more clear as we move away from ‘complicated’ page types like as articles or your main page. If you’re creating a search page, you don’t normally need to include many more items outside the results themselves. The hierarchy is frequently shorter when the structure is more straightforward.

Category and Tag Pages

The WordPress platform produces collective pages for your tags and categories, despite the fact that many websites do not connect to them directly.

There are also distinct subpages for each item inside that taxonomy, which may be found on the main page. Because of how complicated taxonomy may get for websites with vast content libraries, this hierarchy has more’steps’ than is customary. These are as follows:

  1. Archive.php
  2. Index.php
  3. Category–.php
  4. Category-.php
  5. Category.php
  6. Archive.php

It should be noted that the same hierarchy is utilized for tags, with the exception that “tag” is used in place of “category” in all cases. In principle, you may build separate template files for each category or tag on your WordPress website, and then identify them either by slugs or IDs to differentiate them from one another. However, only a few websites really go to the difficulty of doing so. If you don’t want to let people to explore your category page, you may leave thearchive.phptemplate as the default template.

404 Error Pages

Occasionally, visitors will attempt to reach a page that does not currently exist. It is in this case that WordPress will produce an error page (404). WordPress does not, by default, provide any options for altering the appearance of this page. You can, however, customize the look of the document by using the template file provided. The template hierarchy is rather brief: [template name] As the number of visitors to your website increases, the number of times they see 404 errors will increase as well.

This can assist you tell visitors as to why the website isn’t loading and refer them in another way.

Where Child Themes Fit into the WordPress Template Hierarchy

If you enjoy modifying your WordPress themes, one best practice to follow is to utilize a child theme to accomplish your goal. Despite the fact that child themes have a position inside the WordPress template hierarchy, this is not immediately apparent if you glance at the cheat sheet we supplied earlier. Overall, employing a child theme increases the number of layers in the template hierarchy for whatever page type that you choose to utilize. Allow me to illustrate this by assuming you are using a child theme that includes custom template files for single pages.

  1. Pages in the child theme
  2. Pages in the parent theme
  3. Singular.php in the child theme
  4. Singular.php in the parent theme
  5. Index.php in both themes
  6. Index.php in the child theme

If WordPress can locate a version of a template file within your child theme, it will use that version instead of the counterpart found within the parent theme, even if the parent theme contains a counterpart for the template file. In certain circumstances, you may find yourself in a situation where you need to alter the template files for a theme that you are currently using, and in those cases, using a child theme is the best option available. When you update a theme, you’ll lose any modifications you’ve made to the theme’s template files, just like you would with other sorts of customizations.

Using WordPress Template Tags for Theme Development

During the course of WordPress development, you’ll frequently come across template tags, which are PHP functions that may be used to produce and fetch data on the fly.

Template tags are frequently included in template files, however they are not interchangeable with one another. WordPress incorporates a plethora of template tags into its source code, which you may utilize to create your own themes. Here are a few illustrations:

  • Obtain the header and footer of a page, obtain the sidebar of a page, obtain the search form of a page, and obtain the template component.

These tags allow you to collect template files from the theme that you are currently using. For the most part, this implies that you may include certain parts in key template files, such as front-page.php or index php. If you’re looking for a comprehensive list of all of the template tags that may be used in WordPress development, go no further than the WordPress Codex. There you’ll find detailed instructions on how to use each template tag, as well as information on their specific parameters and samples of the code in action.

First, have a look at this tutorial on the template hierarchy.

Summary

Despite the fact that the WordPress template hierarchy appears to be complicated at first glance, it is really just a series of basic decision trees. The WordPress content management system (CMS) detects the sort of page you’re attempting to access and uses the hierarchy to select which template file to load when you visit a WordPress website. It is possible to have template files for specific elements such as headers, footers, and sidebars, in addition to the primary templates for each page type on your site.

Is there anything you’d want to know more about how the WordPress template hierarchy works?

Save time and money while improving the overall speed of your site by implementing:

  • Instant assistance from WordPress hosting professionals, available 24 hours a day, seven days a week
  • Integration of Cloudflare Enterprise Edition
  • With 29 data centers across the world, you can access a global audience. Application Performance Monitoring (APM) is embedded into our platform, allowing for optimization.

That and much more is included in a single plan that includes no long-term obligations, aided migrations, and a 30-day money-back guarantee, among other things. Check out our options or speak with a sales representative to select the plan that is suitable for you.

Beginner’s Guide to WordPress Template Hierarchy (Cheat Sheet)

Following the publication of our infographic on how WordPress works behind the scenes, some users requested that we discuss how a WordPress theme works behind the scenes as well. In this post, we will go through the WordPress template hierarchy for those who are new to the platform. When tweaking a template or building a custom WordPress theme, this cheat sheet comes in handy a lot. Pro Tip: Want to build custom landing pages without having to change your WordPress theme? Check out this tutorial.

Why Learn About WordPress Template Hierarchy?

Templates, stylesheets, javascript, and graphics are all essential components of current WordPress themes. These files work together to determine how your website appears to visitors. Check out these 9 considerations while picking the ideal WordPress theme for your website. WordPress employs a common template hierarchy to organize its content. Essentially, this implies that templates with specified filenames have an impact on specific portions of your website. Moreover, it instructs you on which template name to utilize for a certain item.

By developing a child theme, you may extend the number of templates available to you as a user. After that, let’s have a look at how the WordPress template hierarchy is implemented in the background of the website.

Visualizing WordPress Template Hierarchy (Cheat Sheet)

When naming templates, WordPress follows a simple and meaningful structure that is easy to grasp. Using the visual cheat sheet provided below, you may learn which template files are utilized to show which pages on a WordPress website. Listed below is a breakdown of which WordPress template files should be modified for which pages on a typical WordPress website.

Which template files are used by home page?

WordPress automatically shows your blog entries on the front page of your website when it is first installed. If you want to utilize a custom home page (also known as the front page), you may do so by going toSettings » Readingpage in the WordPress administration section. 1. front-page.php– If you are using a static front page, WordPress will first look for this template file and use it to show the front page. 2. front-page.php– If you are using a dynamic front page, WordPress will first look for this template file and use it to display the front page.

static-front-page.php– If you are using a static front page and you do not have a front-page.php template in your theme, WordPress will seek for a home.php template to use.

3.

This template is the default backup template in WordPress, and it may be used to show any page on the website.

Which template files are used by single post?

To show a single post, WordPress looks for these files in the appropriate folder. Using this template, you can customize the appearance of a particular individual post in any post type. 2. single- -.php– This template may be used to customize the appearance of a specific individual post in any post kind. WordPress would seek for the file single-review-acme-phone.php, for example, if the post type is’review’ and the post slug is ‘acme-phone’ 2. single-.php– WordPress will then check to see if there is a template for displaying this specific post type.

  1. single-.php– WordPress will check to see if there is a template for displaying this specific post kind.
  2. 3.
  3. 4.
  4. 5.

Which template files are used by single page?

WordPress pages are one of the post kinds that are included by default. They let you to generate static pages on your website instead of posts, which saves you time and effort. Take a look at our explanation of the difference between posts and pages. 1. Custom Page Template – This is the page template that has been allocated to a certain page. In this tutorial, you’ll learn how to create a custom page template in WordPress. When the page slug is contact-us, WordPress will seek for the file usepage-contact-us.php to be used instead of page-contact-us.

If the page ID is not 17, WordPress will use the default template file.

4. page.php – This is the template that will be used to show all static pages. 5. singular.php– This template is used as a default fallback for all single post type objects in the database. 6. index.php – This is the default fallback template for the system.

Which template files are used by category archives?

WordPress makes use of these files in order to show category-related pages on its website. 1. category-.php– This template is used to display the category archive page for a certain category. 2. category-.php– This template is used to display the category archive page for a specific category. For example, if the category slug is reviews, WordPress will seek for the file category-reviews.phptemplate to use as the template. Secondly, WordPress checks for a template with the category ID in the category-.php file.

In WordPress, this template is used to display all of the category archive pages, which is called a category archive page.

archive.php – This is the default template that WordPress uses to show any archive pages that are created by users.

index.php – This is the default fallback template for the system.

Which template files are used by tag archives?

WordPress makes use of these files to show tag archive pages on its website. When a tag’s slug is fruits, WordPress will look fortag-fruits.php in the tag-root directory. When the tag’s ID is 17, WordPress looks for the tag-17.phptemplate, which is a tag-specific template. 3. tag.php – This is the template that is used by default for tag archives. 4. archive.php– This is the template that is used by default for any archive page. 5. index.php – This is the default fallback template for the system.

Which template files are used by custom taxonomy archives?

Categories and tags are two of the default taxonomies in WordPress. Users can also design their own own taxonomies using the taxonomy generator. The following diagram illustrates how WordPress searches for templates to show custom taxonomy pages. WordPress will check for the file taxonomy-genre-thriller.php if you have a custom taxonomy named genre and there is a phrase ‘thriller’ in the taxonomy.php. Secondly, if the taxonomy was a genre, WordPress would seek for the file taxonomy-genre.php.

taxonomy.php – This is the default template for displaying any custom taxonomy archives that have been created.

archive.php – This is the default fallback for all archive pages in WordPress, and it can be found here.

index.php – This is the default fallback template in the WordPress platform.

Which template files are used by custom post types?

The following diagram illustrates how WordPress searches for templates to show custom post type archives. When a post type is review, WordPress will seek for the archive-review.php file in the root directory of the site. 2. archive.php – This is the default template for WordPress, and it is used to show all archive pages. 3. index.php – This is the default fallback template in the WordPress system.

Which template files are used to display author archives?

WordPress creates archive pages for each author on your WordPress site, which may be found here. Here’s what the author archive template looks like in action. Firstly, if the author’s pleasant name is Matt, WordPress will seek for the file author-matt.php in the author folder. The second file is author-.php. If the author’s user ID is 6, WordPress will seek for the file author-6 php.

3. author.php – This is the default template in WordPress that is used to display author archive pages. 4. archive.php – This is the default template for WordPress, and it is used to show all archive pages. In WordPress, the index.php template serves as a default backup template.

Which template files are used to display date based archives?

WordPress also shows your content on date-based archive pages for months and years after they have been published on the site. Here’s what it looks like when looking for templates for these pages. 1. date.php – This is the template that is used by default for date-based archives. 2. archive.php – This is the default template in WordPress that is used to display author archive pages. 3. index.php – This is the default fallback template in the WordPress system.

You might be interested:  How To Redirect A Page In Wordpress?

Which template files are used to display search pages?

1. search.php – This is the default page in WordPress for displaying search results. 2. searchform.php – This is the template for displaying a search form in the WordPress content management system. 3. index.php – This is the default fallback template in the WordPress system.

Which template files are used to display 404 error pages?

The 404 error page is displayed when WordPress is unable to locate the content that has been requested. See our advice on how to make your 404 page template more user-friendly. 1. 404.php – This is the default template for displaying the 404 error page in the WordPress content management system. In WordPress, the index.php file serves as a default backup template.

Which template files are used to display attachment pages?

1. MIME type.php – The term “MIME type” refers to the file type. For example, image.php, video.php, and application.php are all PHP files. 2. attachment.php – This is the default template for displaying attachment pages on the web site. 3. single-attachment.php – This file is used to show a single image attachment. 4. single.php – This is the default template for displaying items of the single post type. In WordPress, the index.php template serves as a default backup template.

Which template files are used to display embeds?

Since WordPress 4.5, you have the option of using templates to display a post that has been integrated into WordPress. Firstly, WordPress will search for a post type and post format template in the embed- -.php file. For example, if you have a review that includes a video, WordPress will look forembed-review-video.php in your theme folder. When a post type is review, WordPress will seek forembed-review.php, which is a PHP script that embeds a review. 3. embed.php – This is the default fallback for all embeds on the website.

  • Check out our list of the top drag-and-drop WordPress page builders if you need more inspiration.
  • On top of that, you can follow us on Twitter and Facebook.
  • This means that if you click on one of our affiliate links, we may receive a fee.
  • The Editorial Staff at WPBeginner is a group of WordPress specialists, lead by Syed Balkhi, who provides guidance and support.

The WordPress template hierarchy explained – EasyWP

If you’re interested in theme creation, you should be familiar with the concept of templates. In order for WordPress to know which template files inside a theme to utilize at any given time when someone visits your WordPress website page, the template hierarchy must be defined. Template files within a theme dictate how everything should look. The next level of theme modification will be reached once you understand what the WordPress template hierarchy is and how it functions. You’ll have more control over the appearance of your theme than just adding content and changing the colors.

This guide contains all of the information you’ll need to get started. Introduction to WordPress templates, including a walkthrough of the template hierarchy and explanation of how themes employ template files to display the pages on your website.

Why do you need to know about template hierarchies?

The template hierarchy was created in order to make it easier for anybody to alter the appearance of a WordPress site, regardless of their level of skill with theme development or design. Every time a user visits a page, the WordPress Content Management System (CMS) brings up the appropriate template. When it comes to WordPress, there are seven different sorts of pages that you may utilize to create a theme. These are:

  • The front page, single post, single page, category pages, tag pages, custom post, and search pages are all options.

Each page has its own individual appearance. For example, a front-page seems quite differently from a single page, and a single page appears very differently from a bespoke post. Each page has distinguishing qualities that are established by the template that it is based on. Making changes to themes without having a firm grasp of the template hierarchy is not impossible, but it is time-consuming and difficult. Given that you’re interested in developing your abilities as a web developer, it’s worthwhile to spend some time learning about how things function on the backend.

  • Customize an existing WordPress theme
  • Edit the theme files for children’s themes
  • Or develop a theme from the ground up.

Because themes are made up of a group of template files, you’ll need to know which template file has to be altered in order to carry out any of these functions.

What is the WordPress template hierarchy?

What method does it use to determine which template is being used to produce a requested page? This is where the concept of template hierarchy is introduced. When WordPress is used to show a selected web page on your website, it employs a mechanism called the template hierarchy to decide which template file is necessary. It is incorporated into WordPress and operates in the background, taking only milliseconds to activate when a visitor appears on a page inside your theme. According to the type of web page to be displayed, the files served up will differ.

When a web page is loaded, it will also load a number of template files, each of which instructs WordPress on how to produce the final page in the proper format.

Index.php, header.php, footer.php, functions.php, single.php, and comments.php are some of the other typical template files required to make a web page run properly.

The header.php, footer.php, and sidebar.php files are examples of such files.

An Introduction to template files

After all of this discussion about the template hierarchy, it’s time to get to know the template files themselves. WordPress makes use of a number of programming languages, the most important of which is.php. When it comes to the appearance of WordPress pages, it is the template.php files that are in charge of that. In the.php template file, there are many distinct variations, and each delivers a different outcome. Let’s keep our attention on this page. When your browser requested that this EasyWP blog post be shown, the WordPress content management system (CMS) first had to identify various template files, one of which is named header.php.

As you may expect, the EasyWP blog (which was created using WordPress) has much more than just a header.

According to the image below, a normal WordPress page requires numerous template files that decide the appearance of each element on your site.

Some files can be used across several pages – for example, a header may appear on a single page, a 404 page, and the homepage, among other places. These are referred to as template partials, and numerous files, including the sidebar.php and footer.php, operate in this manner.

How the template hierarchy works

As part of the process of determining which templates to display on each page, WordPress will first determine which theme you are currently using. It is determined which template or collection of templates will be used to create any of the basic seven pages stated above based on an order known as a query string, which WordPress uses. That is an example of how the WordPress ‘template hierarchy’ works. To put it another way, WordPress looks through a carefully organized template hierarchy until it finds the appropriate template file or files from which to generate a page.

  1. A certain order is followed by this fetching operation; it is hierarchical in nature.
  2. Once you’re comfortable with the procedure, you’ll see that it’s extremely time-saving and effective.
  3. WordPress has a modular approach to templates, which allows you to make changes to a single file and have those changes applied throughout your whole website.
  4. Only the index.php file is required to build a functional theme.
  5. The fact that certain template files always take priority over others explains why they are structured into a hierarchical structure.

Walkthrough the WordPress template hierarchy

As a result of this part, we will examine how the WordPress template hierarchy may function in a real-world scenario. As an illustration, let’s look at a fictitious website. Let’s have a look at the EasyWP website. We’ve created a homepage, a few single pages (such as the price page), and a few blog entries to get things started. In this situation, the following template files would be required by your custom theme: This modest collection of templates is sufficient to power the entirety of this website.

Let’s have a look at the remaining pages that you may visit and see which template files they are based on:

  • In order for this blog post, or any other blog post for that matter, to appear appropriately, the single.php template file must be present. In addition, there are no viable alternatives in the file system’s hierarchy, or, even better, there are no other possibilities at all.
  • Page.php might be used for a single page, such as ourPricingpage. If that isn’t available, WordPress will fall back to the default index.php file.

You should be able to see how the hierarchy functions in order to offer EasyWP after reading this example. According on how your theme is configured, the files that are utilized on your site are determined.

Visual overview of the template hierarchy

The template files that are used to build a WordPress page are depicted in this diagram, which is part of the WordPress template hierarchy.

WPhierarchy has an interactive version of this diagram that you can view by clicking here.

Breakdown of the page types cheat sheet

WPHierarchy is a graphic that illustrates how WordPress makes use of the template hierarchy to produce a web page from the template that is currently available. The order template files that are used for some of the most frequent query types are described in the following subsections. With this explanation, you will be able to determine which WordPress template files to update for each page in a normal WordPress site.

Front Page

WordPress searches for the front-page.php file first before returning the homepage. If the file is not available, the system will fall back to the home.php file. Because the index.php file is always there (otherwise, your theme would not operate), if none of the files are available, WordPress will fall back to the index.php file. There is a distinct procedure for each page type inside the template hierarchy. This is how it plays out on the front page of a newspaper: front-page.php is the first of these files.

3.

Template files used for a single post

WordPress articles (such as this one) are classified as single posts in the WordPress database. We indicated before that numerous template files are involved in the rendering of a post, but we were referring to internal aspects such as the header and the footer. WordPress must first determine which template file should be used for the page as a whole before it can proceed to these. To produce a single post, WordPress follows the steps shown below: 1. single- -.php is a PHP script that allows you to create a single page.

  • single.php is the third file.
  • single.php is a PHP script that allows you to create a solitary object.
  • The index.php page As you can see, WordPress looks for a template that matches the post type and slug that you specify first.
  • If neither of these options are present, the script checks for single.php, and as a last resort, it will fall back to index.php.
  • If you wish to keep things simple, you may use a single template for all of your articles; single.php is the template we use for this purpose.

Template files used for a single page

Creating static pages on your website instead of articles is possible since each page in WordPress has its own category. As an example, if we consider the EasyWP website as a whole, we will have our home page. When the link is clicked, the frontpage.php template is loaded. A single page is loaded in order to display the price page as well as the login section. This hierarchy is followed by a single page:

  • Custom template file
  • Page–.php
  • Page-.php
  • Page.php
  • Singular.php
  • Index.php
  • Page–

Template files: page–.php, page-.php, page.php, singular.php, index.php, page–.php, page-.php, page.php, index.php, page–.php, page–.php, index.php, page–.php, page–.php, page–.php, page–.php, page–.php, page–.php, page–.php, page–.php, page–.php, page–.php, page–.php, page

404 Error Pages

Making your own custom error page (which isn’t accessible out of the box with WordPress) is a basic process.

WordPress, once configured, brings up the following 404 templates in the following order:

  • 404.php – The default template for displaying the 404 error page in WordPress
  • Index.php
  • 404.php – The default template for displaying the 404 error page in WordPress

More examples may be found on the WordPress developers blog, which is maintained by the WordPress team.

Working with WordPress’s template hierarchy

Because of the template hierarchy that we’ve seen, WordPress themes and how you engage with WordPress theme development are quite important. Having gained an understanding of its basic workings, you now have the knowledge and expertise to dig in and pick the templates that are appropriate for any given situation. To get started with the template hierarchy, you can either create your own theme from scratch or create a “child” theme from the files of an existing theme. This marks the beginning of the theme development process.

I’d recommend installing theWhich Templateplugin before you get started in order to assist you with this.

Summary

Having a good knowledge of the template hierarchy is vital if you want to make rapid alterations and customizations to WordPress websites. Despite the fact that it looks difficult at first glance, perhaps you have found it to be more digestible. As we’ve discussed, we’ve established a strong foundation by determining which template files are used on which pages and how they take precedence over one another. Having grasped these fundamental concepts, you now know which files to modify when customizing a WordPress theme to suit your needs.

Make a note of this page and keep it close by as a reference guide to the template hierarchies you may meet along the route.

Leave a Comment

Your email address will not be published. Required fields are marked *