Mastering Template Hierarchy in WordPress Themes

Posted on 19th June 2023

The template hierarchy is one of the most important concepts to understand when developing themes for WordPress. The hierarchy is the order in which WordPress will look for template files when displaying content on your site.

What is the Template Hierarchy?

The template hierarchy is the order in which WordPress will look for template files when displaying content on your site. The hierarchy is very important to understand when developing themes for WordPress.

The WordPress Template Hierarchy

  1. First, WordPress will look for a template file with the slug of the post or page. For example, if the post slug is “about-me”, WordPress will look for a template file called “about-me.php”.

  2. If a template file with the post slug doesn’t exist, WordPress will look for a template file with the ID of the post. For example, if the post ID is “123”, WordPress will look for a template file called “123.php”.

  3. If a template file with the post ID doesn’t exist, WordPress will look for a template file with the post type. For example, if the post type is “post”, WordPress will look for a template file called “single.php”.

  4. If a template file with the post type doesn’t exist, WordPress will look for a template file with the taxonomy. For example, if the post is in the “category” taxonomy, WordPress will look for a template file called “category.php”.

  5. If a template file with the taxonomy doesn’t exist, WordPress will look for a template file with the author. For example, if the post author is “mary”, WordPress will look for a template file called “author-mary.php”.

  6. If a template file with the author doesn’t exist, WordPress will look for the general template files in the following order:

  • index.php
  • front-page.php (if front-page.php exists and is not the same as index.php)
  • home.php (if home.php exists and is not the same as index.php)
  • page.php
  • singular.php
  • archive.php
  • date.php
  • category.php
  • tag.php
  • taxonomy.php
  • author.php
  • search.php
  • 404.php

If none of the above template files exist, WordPress will fall back to the index.php file.

How to Use the Template Hierarchy

Now that you understand the WordPress template hierarchy, you can start using it to develop your own themes. The easiest way to do this is to create a file called template-hierarchy.php in your theme and add the following code to it:

<?php

// Template Name: Template Hierarchy

get_header();

if ( have_posts() ) {

	while ( have_posts() ) {
		the_post();
		the_title();
		the_content();
	}
}

get_footer();

This code will display the title and content of the post or page that you are viewing. You can view the code in action by visiting the template hierarchy page on your site.

If you want to display different content on different pages, you can use the is_page() function to check if the current page is the one that you want to display different content on. For example, if you only want to display the title and content on the about page, you can use the following code:

<?php

// Template Name: Template Hierarchy

get_header();

if ( have_posts() ) {

	while ( have_posts() ) {
		the_post();

		if ( is_page( 'about' ) ) {
			the_title();
			the_content();
		}
	}
}

get_footer();

You can also use the is_front_page() function to check if the current page is the front page. For example, if you only want to display the title and content on the front page, you can use the following code:

<?php

// Template Name: Template Hierarchy

get_header();

if ( have_posts() ) {

	while ( have_posts() ) {
		the_post();

		if ( is_front_page() ) {
			the_title();
			the_content();
		}
	}
}

get_footer();

You can also use the is_home() function to check if the current page is the blog home page. For example, if you only want to display the title and content on the blog home page, you can use the following code:

<?php

// Template Name: Template Hierarchy

get_header();

if ( have_posts() ) {

	while ( have_posts() ) {
		the_post();

		if ( is_home() ) {
			the_title();
			the_content();
		}
	}
}

get_footer();

You can also use the is_single() function to check if the current page is a single post. For example, if you only want to display the title and content on single post pages, you can use the following code:

<?php

// Template Name: Template Hierarchy

get_header();

if ( have_posts() ) {

	while ( have_posts() ) {
		the_post();

		if ( is_single() ) {
			the_title();
			the_content();
		}
	}
}

get_footer();

You can also use the is_archive() function to check if the current page is an archive. For example, if you only want to display the title and content on archive pages, you can use the following code:

<?php

// Template Name: Template Hierarchy

get_header();

if ( have_posts() ) {

	while ( have_posts() ) {
		the_post();

		if ( is_archive() ) {
			the_title();
			the_content();
		}
	}
}

get_footer();

You can also use the is_search() function to check if the current page is a search results page. For example, if you only want to display the title and content on search results pages, you can use the following code:

<?php

// Template Name: Template Hierarchy

get_