Taking Advantage of the Template Hierarchy
December 1, 2011 — Code
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.
There are several ways to change your homepage template now:
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.
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: