WordPress Template Hierarchy

Posted in WordPress

 

One of the things that makes WordPress so versatile and robust is the way that pages are created.  Rather than have hundreds of static pages that would all need individual updates, WordPress stores information in a database and places that data into a template when it is called.

Like a typical webpage, HTML provides the structure, CSS provides the styling, and JavaScript will provide the actions/interactivity to the page.  What WordPress does is use PHP to place all of these elements, along with the content, onto template files.

So when you create a blog post, for example, your story along with the title, categories, tags, etc. are all sent to the database.  When you view that blog post, PHP will grab this information and plug it into the blog post template.

When you are creating or modifying WordPress themes, you need to understand how these templates are used and the order in which WordPress calls these templates.  This is called template hierarchy, and there is a lot of different diagrams and infographics documenting the information.

Lets first talk about template files.  Templates are like building blocks.  They are the different parts of a page that get assembled on the displayed web page.  For example, there is a header template file, called header.php, that will be displayed in the Header section on all of your pages.  There is also a footer.php file that will display in the footer section of all of your pages.

This is great because if you need to make a change to your header or footer (or any other template), you only need to change the template and all of your pages will display the changes immediately.  Some other examples of templates are sidebar.php (a sidebar), comments.php (your comments) single.php (a single blog post), page.php and  archive.php.

If you want to create a new template page in your theme, you can copy page.php, rename it and make your changes.  The only template file that is required by WordPress is index.php.  This is your default file.

So, getting back to template hierarchy, if a theme does not have one of these templates, like single.php, for example, WordPress will seek out a replacement template.  This is no random selection, however.  WordPress has a specific order in which it will look.

This is where these wonderful graphics come along to help you figure it out. Here are a few that I found:

WordPress.org chart

Really Cool Hierarchy chart

What you can also remember is that the more specific the template, the higher priority it will have.  Your default page, index.php, actually has the lowest priority in the template hierarchy.  This is why it is the only required template file.

As long as you assign a template to a page when you create it, WordPress will display that page when using that template.  If a page does not have a template assigned, that is when WordPress will go through the list of template files until it finds the best fit, using its template hierarchy.


Other Template Info (for my future reference):

It is also important to know template hierarchy when creating themes, so you will know what to name your template files.

I found this wonderful resource that explains templates, how to modify them and how to name them:

Introduction to WordPress Templates

 

 

 

 

Leave a Reply