WordPress Template Hierarchy

The WordPress Template Hierarchy is a critical part of truly understanding how your WordPress blog works.

Have you ever wondered why some WordPress themes have certain files while others do not?

A WordPress theme requires only two files as I have discussed before, the index.php and style.css. If you took a basic WordPress Theme and deleted all the files except those two in the theme folder, it would still work. However, the pages would basically look the same.

Now the question is, where do these other files come from? And how does WordPress know which ones to look for? This is actually a very simple process. WordPress uses a page hierarchy to choose which files to use for each type of page.

As an example if there is a single.php file, WordPress will use that for the individual (single) post, if that file does not exist, it will use the Index.php file.

There are 10 basic WordPress Theme pages, these are:

  • 404 (Not Found) page
  • Attachment page
  • Author page
  • Category page
  • Date page
  • Search Result page
  • Single Post page
  • Tag page
  • The Main (Index) page
  • WordPress Page

This gives us the ability to customize each type of page. This WordPress Codex page explains the Hierarchy.

Let’s look at the Tag.php page as a more specific example.

The Tag page hierarchy goes like this.

  • The Tag Template with a matching slug. If the tag’s slug were sometag, WordPress would look for tag-sometag.php
  • tag.php
  • archive.php
  • index.php

So if your WordPress theme does not have a Tag.php page you can easily copy the archive.php page to create a custom tag page, if you do not have a archive page, you could copy the index page to start your tag page.

To help you I have created a simple text file that lists the WordPress heirarchy that you can download here.

So with just a little thought and practice it is very easy to create very unique pages for your WordPress Theme.

6 thoughts on “WordPress Template Hierarchy

  1. Architecturally, I think WordPress is a shambles.

    Just look at “sidebar.php” as an example — some crazy design decision went through unchecked and almost everything sits inside an insane nested list…

  2. Wayne, Your point is exactly what makes WordPress work so well for so many, if you don’t like the default way it is laid out, you can change it. No software is perfect.

    I have worked with a lot of software over the last 20+ years, and I can not recall another package that was so useful to novice and experts at the same time.

  3. Man you have got that wrapped up into a short and sweet post! Much better than the lengthy doc found on WordPress.org support section.

    Not cracking on their resource, but sometimes, such as what you have done, you have to extract the hidden treasures found in their docs and shape, mold and republish the key things that are important.

    LOL!!! I am still not making this sound right. I am not wanting to take credibility away from the WordPress support site… Let’s just say that sometimes you can get information overload over there and that it’s really cool what you did here. Nice post!

    Garry Conn

  4. I wish I had read this useful post before launching my last Scriptlance project. My theme was fixed correctly, but I could have done it myself just as easily if I had your hierarchy file first.

Leave a Reply

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