The different template files in a WordPress theme

An essential part of a WordPress theme are the included template files. In this article I give an overview of the different templates and show when which files are used and loaded by WordPress.

What are template files?

Template files are PHP files in the WordPress theme and are responsible for the output of the HTML markup for displaying the website. They mostly consist of a mix of HTML and PHP code.

Basically, a WordPress theme only needs a single template file: The index.php

However, most developers use specialized templates to adapt the display of the theme depending on the context and to store frequently used template blocks in their own files.

There are two types of template files:

Query Templates
These template files are loaded by WordPress at a specific URL request / query. Among other things, for individual posts, static pages or archives.
Examples: archive.php, single.php, home.php, page.php

Template Partial
These files are responsible for displaying a specific area such as the header, sidebar or footer of the website. They are included in other template files.
Examples: header.php, sidebar.php, comments.php, footer.php

When is which template used?

If a page is called up on your website, WordPress loads the correct template file for the theme in the background.

Which template file is loaded depends on the entered URL. A static page is displayed using different template files than, for example, the archives or search results.

WordPress searches for the appropriate template file along the path of the template hierarchy. The mandatory index.php is at the end of the chain and is only loaded if the theme does not have a more specific template for the request.

The template hierarchy thus determines the order in which the templates are used. If template files do not exist, the system jumps to the next file in the hierarchy.

This diagram provides an overview of the complete template hierarchy:

Source: developer.wordpress.org

Primary WordPress template files

The files one hierarchy level above index.php are called primary templates. These are the most common template files used in themes.

404.php

Used when WordPress cannot find the requested page in the database, whether it’s a category, page, or post.

home.php

Used to display the latest blog posts (Blog Index). The name is somewhat misleading because the template is not necessarily used on the home page. If the site has configured a static home page in the settings, home.php will be loaded on the post page instead.

archive.php

The archive template is loaded for categories, tags, monthly archives or author pages. In many themes, however, there are more specific, secondary templates such as category.php or author.php, which are used instead (see next section).

search.php

If the user performs a search, the search results will be displayed using this template.

page.php

To display a single, static page, the theme’s page.php is called.

single.php

Loaded to output single posts.

singular.php

This template was only introduced with WordPress 4.3 and is used to display individual posts for all post types (i.e. posts, pages, CPTs). It is loaded after single.php and page.php in the hierarchy, but before index.php.

The singular.php was created for very simple themes that use the same code to display static pages and posts. Instead of single.php and page.php, only one template file needs to be created.

index.php

The base template, which is only used if no specific template file exists.

Secondary template files

The secondary template files are one hierarchy level higher.

author.php / category.php / date.php / tag.php

This means that individual templates can be used for the different types of archives such as categories, keywords or authors instead of using the general archive.php.

front-page.php

If there is a front-page.php file in the theme, this template is always used for the start page.

It doesn’t matter whether a static page or the latest blog posts were selected as the home page in the options. With the template file, the theme therefore has a great influence on the content of the start page and can, for example, display a widget area instead of the normal WordPress loop.

Without the template file, either home.php (blog index) or page.php (static start page) will be loaded as normal.

Variable template files

WordPress also allows variable template files in the theme, which are the default templates for individual duels Overwrite posts, pages, categories, tags or authors.

Some possible examples:

category-{slug}.php
The News category has its own template, category-news.php, which is loaded instead of the general category.php.

page-{id}.php
For the page with ID 24, WordPress uses the page-24.php template if it exists.

author-{nicename}.php
The admin’s contributions are displayed differently in an extra template author-admin.php.

The variable template files are also very important for providing your own templates for custom post types:

  • archive-{post-type}.php
  • single-{post-type}.php

This allows, for example, for the custom post type with the slug portfolio individual templates for displaying the portfolio can be created with archive-portfolio.php and single-portfolio.php.

You can find a complete overview of all options in the WordPress Theme Handbook.

Standardized template parts

In addition to the query templates presented so far, which are loaded directly from WordPress using the template hierarchy, there are also files for template parts.

Template parts are parts of the template or template blocks that are loaded in one or more other template files. This has the advantage that code is not repeated in several templates, but is defined in a single file.

The following template partials exist in most WordPress themes and are defined with these filenames as the default.

header.php

Shows the header of the theme. Next to the <head> In the website area, the file usually also contains code for displaying the page title / logo and navigation menu.

Loaded from other template files with the get_header() function.

sidebar.php

The template file contains code for displaying sidebar widgets. Is included by other templates with the get_sidebar() function.

footer.php

Contains the footer of the website, inclusive </body> and </html> at the end of the template. Called by other template files with the get_footer() function.

comments.php

This template file takes over the display of the comments. Is usually included in single.php and page.php with the comments_template() function.

searchform.php

Used to display the search form. The template file is only required if the core’s default form for the search field in the theme is to be overwritten.

Loaded with the get_search_form() function.

Own template parts

In addition to the standardized template parts, theme developers can also create their own template parts with any name. Many themes pack these files into an additional folder /template-parts/.

The developer decides how many individual template files a theme is divided into. The new standard theme TwentySeventeen, for example, uses a relatively large number of files for individual template parts.

The advantage of many files is that very small parts of the theme can be modified in child themes (e.g. only the navigation) without having to overwrite the entire file (e.g. header.php).

Instead of using extra functions, own template files are loaded into other template files with the general function get_template_part().

The various functions for loading template files in the theme will soon be presented in more detail in another post.