WordPress Powered

Custom Category, Page and Post Templates for WordPress

Quick overview of custom templates for WordPress Categories, Pages, and Posts. You may already be familiar with Category and Page templates but custom Post templates are just as easy.

DISCLAIMER: Before making any edits to original files make sure to have backups in case something goes terribly wrong…not that it should but you can never be too safe!

WordPress Category Templates

WordPress Category templates are very easy to use, view the WordPress Codex – Category Templates and you will see that the hierarchy for a category template is:

  • 1. category-6.php
  • 2. category.php
  • 3. archive.php
  • 4. index.php

This just means that WordPress will first look for a template labeled category-6, if there is none, it will then look for category.php and so forth all the way down to index.php.

To create a custom category template, just copy your category.php and resave it as category+”your category ID number.” If you do not have a category.php, you can copy your archive.php or index.php but just remember that your custom template will look like the template you copied until you make your edits. So if you have a category.php, archive.php, and index.php you will want to copy your category.php and work from there.

You can find your category ID number by logging into your WordPress backend…go to your categories section and hover over or copy any category names and you will see your category ID. Upload category-“ID#”.php to your theme directory and the specified category can now be freely styled to look differently than your other categories.

WordPress Page Templates

WordPress Page Templates may be easier than Category Templates. WordPress looks for templates in the following order:

  • 1. The Page’s selected “Page Template”
  • 2. page.php
  • 3. index.php

Let’s pretend we want to create a specific template for an “About Us” page. Most themes will have a page.php so once again, you can copy your page.php and resave it, let’s call it about.php.

Once resaved, our page template must start with the following 5 lines of code so add the following to the very top:

<!--?php
/*
Template Name: About
*/
?-->

The above sets a name for your page template, so you can enter just about anything in the Template Name: “Your Choice” For ease of consistency I would label the template name something similar to what I am saving the file, so this about.php will have a Template name of About. Now that you have your Page Template named along with some copied format from your original page.php you can edit this about.php to your heart’s desire. Any edits made to about.php will only affect those pages using this template. Upload your new about.php to your theme directory and we are all set.

To use a Custom Page template just create or edit any page. You should see a dropdown box labeled Page Template, if all is in order you should see your template name as an option in the dropdown.

WordPress Post Templates

WordPress Post Templates are great because they allow you to make posts in different categories different from each other.

For example, let’s say you had 2 categories named Apples and Oranges. You styled each category differently using Custom Category Templates but also wanted posts that appeared in Oranges to have a different look than posts appearing in Apples.

Custom Category Templates do not pass any styling to the posts within, posts take their styling from either single.php or index.php as per the WordPress Template Hierarchy.

Most themes will have a single.php, so yes once again duplicate your single.php…twice. We only need two Post Templates in this example since we want one Post Template for Oranges and we will just be using our original Post Template for posts appearing in the Category Apples as well as for the rest of our posts in our blog.

Let’s name one of our copies of single.php to singleOriginal.php, this is a direct duplicate of our original file, unedited.

For our second copy of single.php, which will be used as a Custom Post Template for all the posts in Category Oranges, let’s save this file as singleOranges.php. This file will be the edited single.php that will have whatever custom edits you want for all Posts in the Category Oranges. Make your edits to this file and you will also need to retrieve the category ID for the category Oranges, let’s pretend it has an ID of 10.

Recap

Before we move ahead, let’s make sure we have everything in order, we should have 3 files and 1 Category ID number.

The 3 files consist of a singleOrignial.php which is a direct duplicate of our original single.php. We should also have a singleOranges.php which has our edits for our custom look for all posts in the Category Oranges (ID #10). And finally, we should still have our original single.php unedited in it’s original format.

Now let’s edit single.php since WordPress first goes to single.php for all Post Templates. Edit your single.php with the following:

   <!--?php
    $post = $wp_query--->post;
    if ( in_category('10') ) {include(TEMPLATEPATH . '/singleOranges.php');}
    else {include(TEMPLATEPATH . '/singleOriginal.php');
    }
    ?>

That’s it! Basically the above checks the posts to see if it’s category 10, if so use the template singleOranges.php, else use singleOriginal.php. What if you wanted a Custom Post Template for Oranges, Apples, and Grapes but still wanted a single.php for everything else?

 <!--?php
    $post = $wp_query--->post;
    if ( in_category('10') ) {include(TEMPLATEPATH . '/singleOranges.php');}
    elseif ( in_category('11') ) {include(TEMPLATEPATH . '/singleApples.php');}
    elseif ( in_category('12') ) {include(TEMPLATEPATH . '/singleGrapes.php');}
    else {include(TEMPLATEPATH . '/singleOriginal.php');
    }
    ?>

A very easy way to customize posts in any category. Go ahead and get creative with customizing then come back and leave a comment to show it off, would love to see what some people do!


share this:

like this:

80 responses to “Custom Category, Page and Post Templates for WordPress”

  1. Eric Avatar
  2. gabediaz Avatar
  3. Adam Avatar
    Adam
  4. gabediaz Avatar
  5. Rebecca Avatar
  6. Adam Avatar
    Adam
  7. Adam Avatar
    Adam
  8. gabediaz Avatar
  9. gabediaz Avatar
  10. Joseph Avatar
  11. Joseph Avatar
  12. gabediaz Avatar
  13. trumoc Avatar
    trumoc
  14. gabediaz Avatar
  15. trumoc Avatar
    trumoc
  16. Chris Avatar
    Chris
    1. gabediaz Avatar
  17. Paul Avatar
  18. gabediaz Avatar
  19. daniel Avatar
  20. Ric Avatar
  21. gabediaz Avatar
  22. Roger Avatar
  23. gabediaz Avatar
  24. Louisa Avatar
    Louisa
  25. Desu Avatar
    Desu
  26. gabediaz Avatar
  27. Arnaud Guillaume Avatar
  28. Lasha Krikheli Avatar
  29. gabediaz Avatar
  30. Lasha Krikheli Avatar
  31. gabediaz Avatar
  32. Lasha Krikheli Avatar
    1. gabediaz Avatar
  33. Dave Larson Avatar
  34. gabediaz Avatar
  35. Dave Larson Avatar
  36. gabediaz Avatar
  37. Dave Larson Avatar
  38. Dave Larson Avatar
  39. gabediaz Avatar
  40. Dave Avatar
  41. Dave Avatar
  42. Dave Avatar
  43. Fahim Avatar
    Fahim
  44. Jason Avatar
  45. gabediaz Avatar
  46. PenPeg Avatar
    PenPeg
  47. Michael Johansen Avatar
  48. Michael Johansen Avatar
  49. Michael Johansen Avatar
  50. Michael Johansen Avatar
  51. gabediaz Avatar
  52. markus Avatar
    markus
    1. gabediaz Avatar
  53. markus Avatar
    markus
  54. markus Avatar
    markus
  55. alat sulap Avatar
  56. Full Specs reviews Avatar
    Full Specs reviews
  57. Shovan Avatar
  58. gabediaz Avatar
  59. Tory McBroom Avatar
    Tory McBroom
  60. Akif Avatar
  61. jenny Avatar
    jenny
  62. Paulo Henrique Avatar
  63. Kiki Avatar
    Kiki
  64. ashocka Avatar
  65. Carlos Avatar
  66. Gabor Avatar
    Gabor
  67. rich Avatar
    rich
  68. rich Avatar
    rich
  69. pankaj Avatar
    pankaj
    1. Gabe Diaz Avatar
  70. Phil Avatar
    Phil

Leave a Reply

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