In WordPress sometimes you want a different look for different categories. It’s fairly easy to accomplish with a little coding and creating custom page templates.
The only necessity for using a Page Template is the below code. Just place it at the top of the page template you are creating. A good point of reference to see how a Page Template is handled is by looking at the file ‘archives.php’ in your theme. It’s usually the second ‘Archives’ link in the theme editor for the default WordPress theme.
1 2 3 4 5 | < ?php /* Template Name: Custom Category */ ?> |
The below code should be placed at the top of ‘category.php’.
6 7 8 9 10 11 12 13 14 | < ?php $post = $wp_query- >post; if ( in_category('3') ) { include(TEMPLATEPATH . '/cat3.php’); } elseif ( in_category('4') ) { include(TEMPLATEPATH . '/cat4.php'); } else { include(TEMPLATEPATH . '/cat.php'); } ? > |
As always you can find out more about creating themes and template tags at the WordPress codex.
4 People Have Bloviated
David Airey | Jan 14
I get so many new items for my ‘to-do’ list from you. Maybe I should unsubscribe.
Jen | Jan 14
Nah, just get a longer piece of paper.
Brady | Feb 8
Hello Jen, I just stumbled across your blog today and started digging through your archives and came across this post and figured I’d let you know that WordPress actually has a built in method for using category templates. It’s called Template Hierarchy, which means WordPress will actually search for very specific templates and gradually fall back to more general templates before it finally decides to use index.php.
So instead of using Page Templates, you could create a category-3.php, category-4.php, and category.php and get rid of the code above. Check out Template Hierarchy and Category Templates in the WordPress Codex for more info. Keep up the great blogging!
Jen | Feb 8
@ Brady- thanks for the tip. Anything to make my WP development easier, the happier I am.