WordPress Tutorials

The template hierarchy in wordpress

The template hierarchy in wordpress

When a user visits your site and navigates to a page, WordPress uses a system called the Template Hierarchy to figure out which file in the active theme should be used to render the page. For example, if the user browses to a single post page, WordPress will look for single-post.php. If that’s not found, it will look for single.php. If that’s not found it will look for index.php.

The index.php file is the fallback for all page loads and along with style.css is the only required file for your theme. More typically, you will have a list of files like:

  • 404.php
  • author.php
  • archieve.php
  • attachment.php
  • category.php
  • comments.php
  • date.php
  • footer.php
  • front-page.php
  • functions.php
  • header.php
  • home.php
  • image.php
  • index.php
  • page.php
  • search.php
  • sidebar.php
  • single.php
  • single-(post-type).php
  • style.css
  • tag.php
  • taxonomy.php

Some files in this list are loaded when you call a specific get function. For example, get_header() loads header.php, get_footer() loads footer.php, and get_sidebar() loads sidebar.php. Passing a name parameter to these functions will add it to the filename loaded. So, for example, get_header(‘alternate’) will load header-alternate.php from the theme folder.

The function comments_template() will load comments.php unless you pass a different filename as the first parameter.

The function get_search_form() will look for the file searchform.php in your theme folder or output the default WordPress search form if no file is found.

WordPress has good documentation for the Template Hierarchy, which lays out all the various files WordPress will look for in a theme folder when they are loaded. You can also take a look at the Twenty Twelve Theme or some other well-coded theme to see what filenames are going to be detected by WordPress. Read the comments in those themes to see when each page is loaded.

When developing apps with custom post types, it’s common to want to use a different template when viewing your post types on the frontend. You can override the single post and archive view for your post types by adding files with the names single-(post_type).php and archive-(post_type).php, where (post_type) is set to the value used when the post type was registered.

Article written by dudecmsadmin


Hi there! I`m admin of community where I share everything from web. Im interested more in webdesign.

Leave a Reply

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

Regular Labs(NoNumber) Extensions are ready for download!
Download Now