Secrets of WP Theming: Part 2

Tagged


When last we met we talked about Using custom templates to add pages to WP that don't necessarily call for posts or categories, etc.  Today we continue our exploration of custom templates by looking at another little known power hidden within the theme system: Per Category templates.


Hidden Spiffiness abounds


Deep within the recesses of the WordPress code base lies dormant the power to create custom templates for each of your categories, what is even more astounding is how easy it is to pull off.  This sleeping giant merely awaits the correct file names and it will wake unleashing untold shininess and spiffiness upon your visitors heads!


It should be said that this isn't a feature that we are all going to want to run out and use, but I think that for a select few this will be a a breath of fresh air, not to mention a solution to many frustrations.


When you get down to brass tacks, this is a profoundly easy feature to exploit.  All you need to know is the number of each category and you know, what you want each one to look like.  So lets look at how I am using this feature here on Sillyness.

Context sensitive information is the bees knees


I am a big believer in giving information to the user that is both contextually correct and also useful.  A prime opportunity for this is when a reader is browsing your category pages.


Here on Sillyness where appropriate you will find in the left aligned column a list of "other resources" specific to the category you are browsing.  Two prime examples are the Tutorial and Web Standards categories.  On each one you will find links to offsite resources.


Now this is a very limited example of per category templates.  If you wanted you could change every aspect of the way the page is displayed, load alternate style-sheets anything... the sky is the limit.  But think before you go crazy people.


So what does one need to do to tap into the power of per category templates?  Glad you asked!

So lets write some code, shall we?


After the previous discussion, creating these templates will be a piece of cake.  First of all lets start off by making sure we actually have a category.php file to begin with.  I find it very helpful to have a good layout with which to browse categories, and you readers will too.


So let's say we are all brilliant and already have a category template file, we need to open that puppy up in an editor and decide what we want to customize for lets say... the gorilla category.  Heh, gorillas are funny.


Okay so we are going to create a template for our gorillas category (cat number 18).. okay?  Good.  So I think we need to have some links to gorilla resources on the net offered to our visitors while they are in our gorilla category listing, don't you?


So lets get to tinkering , first we throw out an oldy but a goody:



Next we call for our header.php file and start adding our context specific info:



Now usually we would want to just call and include the template file sidebar.php, but we are wanting to add that context info and the menu is the best place for it.  So we will be hardcoding this bit on each template.



Then we go on and throw out the template tags that tell WordPress how we want to display our listing on the category page:

 div class="main">

<?php the_excerpt(); ?>

<?php endwhile; else: ?>

<?php endif; ?>



And finally we close with the footer call:


Now for the payoff, making it all work


Okay, so now we have created a template for our gorilla category, complete with spiffy links about gorillas for our readers.  Now we are ready to save this bad-boy, but how does WP know to use this when gorilla is called?


Now it is time to do something with that category number we found.  All you need to do is save this file as category-n.php where n is the number of your category.  So since gorilla is cat number 18 we would save this file as category-18.php and upload it to our wp-content/themes/mytheme/ directory.


Easy-peasy as they say.  Believe it or not, that is all there is to it.  The next time you load the gorilla category you should see the template we just created.


Well that's it for this installment of the series.  I hope you have had as much fun as I have.  If you have any questions, leave them here in the comments and I will do my best to answer them.