page-attributes

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.