Understanding The WordPress Template Hierarchy

Template Hierarchy:

1)      Which template files will WordPress use when it displays a certain type of page?

The General Idea

WordPress uses the Query String — information contained within each link on your web site — to decide which template or set of templates will be used to display the page. First, WordPress matches every Query String to query types — i.e. it decides what type of page (a search page, a category page, the home page etc.) is being requested. Example https://www.digamberpradhan.com.np/?s=asdf this hyperlink will result in a  search page.

Templates are then chosen — and web page content is generated — in the order suggested by the WordPress Template hierarchy, depending upon what templates are available in a particular WordPress Theme.

WordPress looks for template files with specific names in the current Theme’s directory and uses the first matching template file listed under the appropriate query section below.

With the exception of the basic index.php template file, Theme developers can choose whether they want to implement a particular template file or not. If WordPress cannot find a template file with a matching name, it skips down to the next file name in the hierarchy. If WordPress cannot find any matching template file, index.php (the Theme’s home page template file) will be used.

The Template Hierarchy in Detail

https://codex.wordpress.org/Template_Hierarchy

Home Page display

Template file used to render the Blog Posts Index, whether on the site front page or on a static page. Note: on the Site Front Page, the Front Page template takes precedence over the Blog Posts Index (Home) template.

  1. home.php
  2. index.php

Front Page display

Template file used to render the Site Front Page, whether the front page displays the Blog Posts Index or a static page. The Front Page template takes precedence over the Blog Posts Index (Home) template.

  1. front-page.php – Used for both Your latest posts or A static page as set in the Front page displays section of Settings -> Reading
  2. Page display rules – When Front page is set in the Front page displays section of Settings -> Reading
  3. Home Page display rules – When Posts page is set in the Front page displays section of Settings -> Reading

Single Post display

Template file used to render a single post page.

  1. single-{post_type}.php – If the post type were product, WordPress would look for single-product.php.
  2. single.php
  3. index.php

Page display

Template file used to render a static page (page post-type)

  1. custom template file – The Page Template assigned to the Page. See get_page_templates().
  2. page-{slug}.php – If the page slug is recent-news, WordPress will look to use page-recent-news.php
  3. page-{id}.php – If the page ID is 6, WordPress will look to use page-6.php
  4. page.php
  5. index.php

Category display

Template file used to render a Category Archive Index page

  1. category-{slug}.php – If the category’s slug were news, WordPress would look for category-news.php
  2. category-{id}.php – If the category’s ID were 6, WordPress would look for category-6.php
  3. category.php
  4. archive.php
  5. index.php

Tag display

Template file used to render a Tag Archive Index page

  1. tag-{slug}.php – If the tag’s slug were sometag, WordPress would look for tag-sometag.php
  2. tag-{id}.php – If the tag’s ID were 6, WordPress would look for tag-6.php
  3. tag.php
  4. archive.php
  5. index.php

Custom Taxonomies display

Template file used to render the Archive Index page for a Custom Taxonomy

  1. taxonomy-{taxonomy}-{term}.php – If the taxonomy were sometax, and taxonomy’s term were someterm WordPress would look for taxonomy-sometax-someterm.php. In the case of Post Formats, the taxonomy is ‘post_format’ and the terms are ‘post-format-{format}. i.e. taxonomy-post_format-post-format-link.php
  2. taxonomy-{taxonomy}.php – If the taxonomy were sometax, WordPress would look for taxonomy-sometax.php
  3. taxonomy.php
  4. archive.php
  5. index.php

Custom Post Types display

Template file used to render the Archive Index page for a Custom Post Type

  1. archive-{post_type}.php – If the post type were product, WordPress would look for archive-product.php.
  2. archive.php
  3. index.php

(For rendering a single custom post type, refer to the Single Post display section above.)

Author display

Template file used to render an Author Archive Index page

  1. author-{nicename}.php – If the author’s nice name were rami, WordPress would look for author-rami.php.
  2. author-{id}.php – If the author’s ID were 6, WordPress would look for author-6.php.
  3. author.php
  4. archive.php
  5. index.php

Date display

Template file used to render a Date-Based Archive Index page

  1. date.php
  2. archive.php
  3. index.php

Search Result display

Template file used to render a Search Results Index page

  1. search.php
  2. index.php

404 (Not Found) display

Template file used to render a Server 404 error page

  1. 404.php
  2. index.php

Attachment display

Template file used to render a single attachment (attachment post-type) page

  1. MIME_type.php – it can be any MIME type (image.phpvideo.phpapplication.php). For text/plain, in order:
  2. text.php
  3. plain.php
  4. textplain.php
  5. attachment.php
  6. single-attachment.php
  7. single.php
  8. index.php

 

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.