As someone who does mostly custom work for clients and for my own projects, I am always thrilled at the constant improvements that are made to the WordPress template hierarchy. I can remember back to a time when having a page load first instead of your latest posts was a moderate hack instead of a simple setting. Then there was the template hack of getting a custom layout on your homepage. It’s not really much of a hack but I consider it a rusty idea now that we have much better methods.

Custom Home Page Template

There are several ways to change your homepage template now:

  1. Create a page template and selecting it from the “Page Template” dropdown in the page attributes bar as pictured to the right.
  2. Create a page template that is named with the ID of the page. So if your Home page has an ID of 2, you would name the template “page-2.php”.
  3. Create a page template that is named with the slug of the page, or “page-home.php”.
  4. The last and best way is to simply name the template “front-page.php” since this is the page you are setting as your “front page”.

Template Conventions

The hierarchy rules for everything else follow pretty much the same rules. If you haven’t taken the time to study the Template Hierarchy chart in the Codex, then I’d say you’re not really serious about building themes. It’s absolutely required reading.

Category Templates

One more area that I want to especially take time to point out is how category templates are handled. A couple of years ago I wrote a blog post on how to create a page template that paginates posts from a certain category. It has been one of the most popular posts on my blog and also one of the hardest pieces of code to get to work in everyone’s particular situation as you can tell by the multitude of support requests in the comments. In fact, it’s the very reason I’m writing this post.

My original intention was to show how I had hijacked a category called “portfolio” in order to add blog posts to my site that ere filtered from the main feed areas and lived in their own portfolio section on the site. Since then, I have moved my portfolio items into a much better application by creating a custom post type for them, but I digress.

My official recommendation for customizing a page of category posts is to simply customize that category’s template. Just like the home page templates, you have multiple options:

  1. Create a category template that is named with the ID.. So if your Tutorials category has an ID of 2, you would name the template “category-2.php”.
  2. Create a category template that is named with the slug, or “category-tutorials.php”.
Doing this will save you a ton of headaches since you won’t have to worry about creating a custom post query, or pagination rules.

14 thoughts on "Taking Advantage of the Template Hierarchy"

  1. sam says:

    You seem like a very smart person who could help me with what i very much want to do…why do you make instructions inscrutable…this is my personal favorite…you write

    ” First off, you’ll need to start with a custom template, and the easiest way to do that is duplicate your normal page template, page.php. In this file, add the following code above the call for the header:”

    wtf? why not just write it in a way a normal person can understand? WHICH FILE? where in wordpress? what do i have to type? it cant be that complicated. wish folks like you would explain clearly

    1. Tammy Hart says:

      I wish folks like you would write your comment in a way that I could understand, because the code that should be added follows that sentence in that tutorial. Also… You shouldn’t even be using it as it clearly states it’s old and outdated.

  2. Art Webb says:

    Thanks for your help in explaining this amazing feature in WordPress.

    My questions are:
    1. what PHP content is presented in a category template?
    1a. where do you find this content?
    2. what directory does the category template exist in?
    3. could you possibly elaborate an updated step-by-step approach for your new method?

    1. Tammy Hart says:

      No problem. Here are my answers:

      1. The category template outputs an index of posts within that category with the Loop.
      1a. They are posts found within the normal posting functionality of WordPress
      2. all WordPress templates are found in the main directory of your theme
      3. The template hierarchy does most of the work for you. Rather than explain everything and hope that I answer your question, can you tell me specifically what you need to know? Definitely check the codex page as well.

  3. Art Webb says:

    I need to create a post category as a menu option (ie: downloads). Right now WordPress creates URL’s for a category like this: example.com/category/downloads/ — I need it to look like this: example.com/downloads/ — which I just did, thanks to your previous tutorial. I simply pasted your “Template & Loop” code right above my theme’s index.php code (with the loop cat-id as “downloads”) and it worked. I named the file as template-downloads.php.

    How can I “take advantage of the Template Hierarchy” with your new method? Or am I golden with my current method?

    By the way, for my theme I’m using paid themes by YooTheme, which has a very slim index.php file. It calls upon a template.php file, and that calls upon a template.config.php file.

  4. Art Webb says:

    In case anyone is wondering how I got rid of /category/ from the URL, I did the following:

    1. I made a PAGE called “downloads”
    2. I added that PAGE to my menu bar
    – the url now reads example.com/downloads/
    3. I created a template file by copying my template’s index.php file and adding Tammy’s code above the content
    – this differs by template, I used the template code and LOOP code
    4. I added template-downloads.php to theme root folder
    – theme root is here: example.com/wp-content/themes/YOUR THEME/
    4. I selected the template by clicking the page’s “quick edit” feature
    – you can only change your template AFTER you create a new template
    5. I posted in the category “downloads” — and it worked!

  5. A.Barlow says:

    Question, would I be able to do this to create to distinct blogs that I could post to by selecting a specific category?

    I would like main blog, and then another blog with posts that would remain separate from the main blog.

  6. Brandon says:

    Tammy,

    I think you are just the person to help me! I have searched high and low via google for a solution to my question.

    I need to have a New Products page show just posts for that category – New Products as well as subcategories; Birds, Cats, Dogs etc.

    I created a template called New Products, how can I have just this category data appear on this page. I don’t want every category to appear here.

    Thank you much, in advance.

    Brandon

    1. thdadmin says:

      The page already exists because WordPress creates an archive page for every category: http://journalmultimedia.com/petage/index.php/category/new-products

      1. Bradford says:

        I’m wondering how best to take what WordPress already provides (i.e. category/new-products) and make that available via a different URL (i.e. products/new).

  7. Johanna says:

    Bradford:

    You can just use something like a redirection plugin. http://wordpress.org/extend/plugins/redirection/screenshots/

  8. Steve Kwon says:

    Hi Tammy,

    I have a question related to this topic. I want to designate a specific menu item for my homepage, but my theme doesn’t allow for me to to do that in the dashboard. It only allows me to designate pages and not category items for my homepage. Is there a way to designate Features as my homepage?

  9. Steve Kwon says:

    Never mind Tammy. There was a theme option in the dashboard that I missed.

  10. Jack says:

    Hi Tammy!
    I copied the category.php code content inside a new template file. I named this file with the slug of the category i want to create a page with (category-music.php). Then i used the newly created template file as a template of a new page (“Music” page) but it didn’t display any of the posts from that category.. Do you think i reproduced the steps correctly?

    Thank you

Leave a Reply

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