WordPress Template Hierarchy

WordPress theme files are essentially the building blocks for the design of your website. They allow you to modify the look and feel of your website. WordPress uses a database to store content. When you create content, it is stored in a database and displayed using templates when visitors access the content. A WordPress theme…

WordPress theme files are essentially the building blocks for the design of your website. They allow you to modify the look and feel of your website. WordPress uses a database to store content. When you create content, it is stored in a database and displayed using templates when visitors access the content.

A WordPress theme is a combination of files and scripts that presents the database content. WordPress uses a number of files in a theme to render different pages. The files makeup what is called a ‘template hierarchy’ it is used to create and extend WordPress themes. A template is a single theme file that determines how a page should look like in the front end. When you change a WordPress theme, you are basically changing how the content is displayed and not the content itself.

When you look at the homepage you will see the header that is generated by the header.php template, content pulled from the database, the sidebar generated by sidebar.php and the footer generated by footer.php All of these files combined will control how the homepage will look.

WordPress Theme Files

WordPress themes use different templates files to generate a complete web page. For example, when you access a WordPress website, you can actually see multiple different template files combined together to create one complete page.

A Basic WordPress Theme

When you access a single page on the same website, the same header, footer and sidebar are displayed from the same files. The content is created from the single.php file. How do you know which template files are used at what point? That is what the WordPress template hierarchy is for. It determines in what order the template files are going to be loaded.

Naming Convention

WordPress uses a strict naming convention. Header files must be named header.php and footer files footer.php In the theme’s directory, WordPress will look for these files and determine which ones to use. If there is no matching name, the next file name in the hierarchy is searched for. If still not found then the index.php file will be searched

Therefore, when editing templates, you have to keep in mind what type of content it is.

Minimum Theme Requirements

For a WordPress theme to work, only the index.php and the style.css files are required. index.php is required to process WordPress queries and the structure of the site as well.

The only files that are required for a WordPress theme to work are index.php and style.css. The index.php is the main and only file that will process every WordPress query, as well as the structure of the site.

The minimum files required for a WordPress theme to work and to be visible in the WordPress dashboard for activation are index.php and style.css.

Here is the list of the base theme files recognized by WordPress:

  • style.css
  • rtl.css
  • index.php
  • comments.php
  • front-page.php
  • single.php
  • page.php
  • author.php
  • archive.php

Twenty Fifteen Template Hierarchy Example

If you have activate the default 2015 WordPress theme and a user visits : www.yourwebsite.com/author/admin WordPress will search for the file author-admin.php if that does not exists, it will look for author.php if that also does not exist it will look in archive.php. This file is present in the 2015 theme. If you have modified the theme and the file is not available then WordPress will look in the index.php file to render the page.

Whenever a user visits a page on your website, WordPress looks upward in the template hierarchy until it finds a matching template. How files are named affect how they are searched for.

Other useful resources for WordPress template hierarchy:

https://developer.wordpress.org/themes/basics/template-hierarchy/
http://codex.wordpress.org/Theme_Development#Template_Files_List
http://www.sitepoint.com/wordpress-theme-development-getting-started-with-underscores/