Wordpress Plugin: Category Templates
NOTICE: Support for this plugin has been stopped indefinitely. I simply don’t have the time to devote to working on updates and bug fixes.
If you’re interested in adopting this plugin to continue development/support, contact me.
Category Templates is a Wordpress plugin that lets you define different templates for both categories and individual posts through an easy to use interface.
I got the idea for this plugin when creating my own website with Wordpress. I wanted to be able to have different looks for my Portfolio, Projects, and Blog posts. I originally hard coded a few functions, but now I’ve created a simple and easy to use plugin that lets you define what templates to use for you categories and posts.
Due to multiple requests, 2.0 saw the addition of being able to apply templates to category archive pages as well as posts.
What does this plugin do?
When Wordpress is displaying a single post, it uses the template_redirect() function to determine the path of your theme’s single.php file. What this plugin does, is adds a filter to that function, and depending on your settings, chooses the appropriate template file based on the post. If the post or its categories don’t have a template defined, then template_redirect() falls back to its default (usually the single.php file in your template directory).
Category Templates also has a small footprint in the Wordpress database. All data is stored within two rows of your options table. I plan add the ability to delete these rows for when/if you want to uninstall in the next version.
Templates
Valid templates are files within your theme’s directory (/wp-content/themes/{your theme}/) with the following code in them (usually at the beginning):
For Post Templates:
1 2 3 4 5 | <?php /* Template Name: Blog Template */ ?> |
And for Archive Templates:
1 2 3 4 5 | <?php /* Archive Template: Blog Template */ ?> |
You can place them anywhere in your theme directory, even in sub-folders, as long as the above code is at the beginning of the file. If you recognize this method, then bravo (or brava – take your pick): this is the same way Wordpress uses to implement page templates (excluding the ability to place templates in sub-folders). When Wordpress loads all the files within your active theme directory, it searches for the ‘Template Name:’ string within a comment. If found, Wordpress declares that file as a template.
So to add a new template, simply upload the file anywhere in your active theme directory. Yup, it’s that easy.
If you’re not sure how to make a template for a post, I suggest taking a look at the single.php file in your theme’s directory. That is the default file that Wordpress calls when a single post is being displayed, and is a good starting point to go from.
Option Panels
Through the plugin’s options panel (located under the appearance tab), you can choose which templates are applied to categories, along with their priority level.
By checking the apply to sub-categories box for a category, all sub-categories of that category will have that template applied, with the defined priority. For instance, in the image above, the Projects category has a template with a medium priority, that is also applied to its sub-categories. However, the sub-category Music has a template with a high priority. Because Music’s priority is higher, that template will be used. However, if it was lower, then the Projects category would be used.
If a post has multiple categories with the same priority, then the category with the lower ID will take priority.
When adding and editing posts, there is also a meta box for you to select a template for that specific post. However, please note that templates defined here will override any templates associated with the post’s categories, regardless of priority.
This is the first release of this plugin, so use at your discretion. And please let me know in the comments below any suggestions, comments, or bugs you come across.
Download
Download Stats
| Today: | 2 |
| Yesterday: | 8 |
| This Week: | 44 |
| Total: | 3,508 |
July 22nd, 2009 // Plugins |


(4.29/5) 7 votes.
The plugin is not working with version 2.9.1..any update ?
Thanks for this wonderful pluting
Fantastic plugin!!
I found that it conflicted with the Custom Post Templates plugin I was already using, but fortunately you have that same functionality built in to this plugin. I’m sorry to hear you won’t be supporting it anymore, but hopefully the community will keep posting fixes as WordPress progresses. Great work, and thank you.
I got the following message/warnings after upgrading to WP 2.9 RC1 Anybody have any tips on fixing this? Please..
Thanks much!
Warning: file(C:\wamp\www\wordpress/wp-contentC:\wamp\www\wordpress/wp-content/themes/black-hat/404.php) [function.file]: failed to open stream: Invalid argument in C:\wamp\www\wordpress\wp-content\plugins\category-templates\cat-templates.php on line 153
Warning: implode() [function.implode]: Invalid arguments passed in C:\wamp\www\wordpress\wp-content\plugins\category-templates\cat-templates.php on line 153
I’m having the same problem with 2.9. Any ideas for a fix?
I had the same problem after upgrading to 2.9 and managed to fix it by going to line 153 of the plugin and changing
$template_data = implode(”, file( WP_CONTENT_DIR.$template));
to
$template_data = implode(”, file( $template));
It seems it was prepending the full path of the file (WP_CONTENT_DIR), but the $template variable already had the complete path.
Hi, i did the same and the error is gone..i am checking it right now. Thank you so much.
I confirm that this correction solve the problem on wp 2.9.1
Be carreful to type” instead of ”
$template_data = implode(”, file( $template));
thanks
everything loads perfectly wp tables are made, but it doesn’t work
I have the feeling its not working with child themes. I’m using hybrid theme and the news child theme. I i place them in the child folder they are recognized and also the main theme folder. but it doesn’t work proparly. One funny thing. it worked once or twice.. but now not anymore…
wordpress 2.9 error , wish fix the problem
Great plugin, is it possible to have templates for tags?
HI, i installed this plugin but on admin side in category templates there is only menu, nothing else. Someone else posted similar problem. Any idea? Thanks
It did work but now I get this also? Nothing changed that Io am aware of
Hi, I’m using this plugin and am having a problem.
When I tried to use “get_the_category()” in a single post page, it returned no value. When I used this plugin for category archive pages, I had a similar trouble that the variable “$cat” returned nothing.
Now I’m using “Idealien Category Enhancements” plugin to manage templates for category archive and I never experienced such problem.(WP 2.8.4) Do you have any ideas to fix the problem?
Thanks!
Thanks for this wonderful plugin. The current version (2.1) creates some bugs in my template – the videos and custom fields dissapear. Is there a way that we can download the previous version (1.04)?
Thanks in advance
I have the same problem, custom fields dissapear. Is any solution out there? Thx..
I solved the custom fields problem with the v1.0.4 it looks like a global WP variables issue.
http://wordpress.org/extend/plugins/category-templates/download/
Thank you very much for this wonderful plugin, especially now with version 2 !!
If you want to make it compatible with WPML just need to add the following line after “cat_temp_options_page” opening:
$_GET['lang'] = ‘all’;
That’s all, it will show the categories in all the languages defined.
Thanks again.
Adrián
Just checking in to see if there was any headway on the new version.
I am about done. Just smoothing out little things here and there.
Did you still want to help out testing it? If so, I’ll email you later today with a copy of it.
awesome. can’t wait!
Hey there – either we found a bug or … well, we really don’t know!
We’ve installed the category plugin and it appears to work – we can choose which template goes to which page in the admin … however, it just doesn’t work on the front end. We’ve got custom templates running on the site and other plug ins of course but since the plug in appears to work in the admin we’re thinking it’s not a conflict. Does the plug in require anything special in the templates? We’ve added the required code at the top of the pages. We purposely took out a lot of the default WP side bar junk .. would that have anything to do with it? We’re at a total loss here and really need to find a solution. Any insight would be appreciated. Thanks
Thank you so much, now I finally understand how it works! Great!
Hello,
I installed the plugin on my blog but all the options it gives me in the admin panel are the WP Default template and the template that I currently use, no matter which template I choose of the 5 I totally have. I have PHP 5.
I added the code on the single.php of each template, the way you indicated. It still didn’t work. Then, thinking that the Default shows up no matter what, on the list, to change the code, exactly like on that one.
To my recollection, this was the first plugin I installed so I don’t know if it involves a conflict with anything.
I am not a .php expert, so I must have done something wrong myself but after a whole week of trying, I still cannot figure out what is going on.
Can you please, help me with this?
I may just be misunderstanding you, but do you mean that you have 5 themes, and you added the code to the
single.phppage in each?If so, this plugin only works with your currently active theme, so only the pages in your currently active theme (and with the template code) will show up on the admin page.
Thank you so much for your quick reply. I think I finally understand… but to make sure I do, I have a few more questions:
1) I should actually have a folder/subfolder structure, like:
themes / Blue_Theme / orange_theme
_____________________ / white_theme
_____________________ / pink_theme
_____________________ / green_theme
with a single.php containing that little piece of code at the beginning, in each subfolder?
2) Will this change the appearance completely, for each category, I mean from layout to images (background, header, buttons, etc.)? – That’s what I want to happen!!!
2) Do I also need to add the code at the top of other .php files like page.php, index.php, etc. on each sub-theme or not?
Thanks again!
Did you have any luck getting templates to apply to the archives pages?
I keep checking back each day but have not seen any new comments or posts about this yet. I’m still very interested!
I’m still working on adding archive templates. I have a bunch of time this weekend to devote to working on it, so hopefully sometime next week I’ll have the update ready.
Now that is fantastic news!
If you need any testing done this weekend or next week just let me know. I have more than enough time available to help out. I’m not sure if you use wp-mu on your system. I have it running a number of sites and can provide a decent testing ground.
Applying templates to the category archive pages is exactly the feature I have been searching for. Is there an e.t.a. on when this feature may be added?
My idea is simple: Create a multimedia website where artists can upload their own audio/video files. I would create multilevel categories such as:
Artists
–Artist
—Album
—-Song/Video Article
If I had the ability to assign custom templates to categories, then I could easily make the ‘artists’ top level listing look totally different than the ‘artist’ or ‘album’ listing pages.
Really the only hack I would have to implement is to hide ’sub-categories’ from showing up on category listing pages.
That would open up some huge doors with Wordpress.
I’ve gotten quite a few requests to add the ability to apply templates to archives, so it’s something I’m starting work on.
I’ll probably have a release ready within a week or two.
That is the best news I’ve heard all day. Thank you very much!
I can see a lot of people really benefiting from this as it opens so many doors.
Awesome plugin, but I’d like to see custom templates for the post archives. I downloaded this plugin because I thought that’s what it was for. Can’t wait for the next version to have that feature!
Hello, nice plugin. It works fine for posts templates, but I’m trying to make it work for categories and it doesn’t seem to work. I asigned the plugin at Settings for Category Templates and it takes the post template, not the category template. Can you help me please?
If you assign a post to an individual template (via the metabox on the post edit page), that template takes priority over any template you assign to that post’s category. If you want to apply a template to the post based on its category, don’t assign a template to the individual post.
Thanks for your quick answer.
I understand what you say but that is not the problem. I created a Template called “actividades” and I asigned it to a category called “actividades” at Settings for Category Templates. So if you visit a post under that category, you’ll see aplied this template. (http://pcd.thelight.cl/?p=39) But if you visit the category called “actividades” (http://pcd.thelight.cl/?cat=4), it doesn’t take amy template. How can apply another one to it?
regards
This plugin doesn’t apply templates to the category archive page right now. Only to posts depending on what category they’re in. Sorry.
I do plan on including the ability to apply templates to category archives soon thought.
ok thanks a lot for your help
very nice plugins you develop
regards
Hi there;
Same problem here – loaded the plugin, checked that all templates have therequired commented-text in them, and when I go to the Category Templates area of the appearance tab, I get everything in the screenshot you’ve shown, but only up to the bottom of the horizontal grey bar… that is, no caegories whatsoever are listed.
I’m going to uninstall and re-install and see how I go. Disabling other plugins isn’t an option – the yall have to be able to live together.
Cheers
GT
Great Idea and potentially what it can do, thanks.
However, I have an issue. The plugin enabled, Templates Shows up for selection. However No post shows up, post page is blank after plugin is enabled.
I have disabled all plugins, and am using the Hybrid Theme from themehybrid.com.
Great plugin! I’m excited to get to it!
One (hopefully quick) issue — after activating, I am unable to edit existing posts or view the plugin settings. both return the following error:
Fatal error: Call to undefined function: str_ireplace() in /wordpress/wp-content/plugins/category-templates/cat-templates.php on line 155Any idea what i should be fixing?
str_ireplace()is PHP 5 function, so you must have a lesser version. I’m correcting this in the next version.For now, you can simply change it yourself to
str_replace. That should do the trick.Just fixed that and it worked! …and then I saw you updated the plugin, too! I’ll change to that one now
Great! Glad that worked!
Hello,
Great plugin! However, I’ve added the code to my template file, and only “No Template” and “Default Template” appear in the list.
What am I’m doing wrong?
I’m using Wordpress 2.8.4
Here’s a few things to try:
Make sure you added the code to your template file:
<?php/*
Template Name: Blog Template
*/
?>
You can change Blog Template to whatever you’d like, but the important part is to keep Template Name: the same, as the plugin searches for that sting.
Also, make sure you put the template file into your currently active theme folder (wp-content/themes/templatename/).
Let me know if you’re still having issues.
I’ve check all those things, but my template is still not listed.
I also have other problem I didn’t see before. All the links of my site are redirected to the home page when your plugin is enabled. When I disable the plugin, my posts are shown as usual.
I’m using many other plugins and I think there is a compatibility problem with another plugin. I will try to find which plugin it is.
what this looks like is a way to use other WP themes on a certain page with all their functions, but you mention a singular template page to put the commented code at the top of.
You also mention putting the template inside your active theme folder.
So say my overall WP theme name is “Slider” there is a theme folder called slider in the WP-content/themes/
Now adjacent to Slider is another complete theme for WP called “Bluebell”
Are you saying that I need to put the complete Bluebell theme folder inside /Slider ?
When I activated this plugin all URL’s where pointing to the latest post. I deactivated it and it is fine without the plugin.
Thanks! Your plugin works perfect, but when PHP error messages are turned on, I get this:
Warning: Invalid argument supplied for foreach() in /path/to/my/site/httpd.www/wp-content/plugins/category-templates/cat-templates.php on line 91Warning: Invalid argument supplied for foreach() in /path/to/my/sitehttpd.www/wp-content/plugins/category-templates/cat-templates.php on line 99See the URL: http://wordpress.org/support/topic/304680?replies=1
Sorry about that! I didn’t typecast the variable in the foreach() statement, so when it wasn’t an array (in your case), it returned that error.
Should be fixed in 1.0.4 now.
My hero! Thank you!!!
Not sure you offer this.
I’m looking for a plugin that takes category pages and displays them from this http://boxedindesign.com/category/inspirations/ to this http://boxedindesign.com/top-lists/. Everything I download seems to be too old, but yours is the newest I’ve seen and looks like what I need, but I’m not sure. Is your plugin what I need or is there one you could recommend? I’m no expert at WP so there is a learning curve.
Thanks!
This plugin only applies templates to posts depending on what category they’re in, not the category listing page. I don’t know of any plugin that does what you want. You would probably have to hard code it yourself.
If you are wanting all your category pages to look the same, then you can edit the archive.php file and adjust it to how you’d like.
Hmmm I thought I might have to hard code it. Well, thanks for the quick reply man
I will start coding. If you ever get the chance, try your talents at my idea. I’m sure others would want it too.
I may add that to the next release of Category Templates! Thanks for the idea!
Hi, in the dashboard on my 2.8.2 I find the Post Template option, but I can only choose between Default Template and Archieve Page. Have I misunderstood, I have built a variant of my theme, and hoped it could be assigned to posts in specific categories. But it doesnt work like that?
Ops… sorry, I sorted it out! Works like a charm, thank you!!!!
No problem.
Do you know what happened, or what you did you fix it?
I’ve installed the plugin and am having a problem too.
I have the administration panel and selected my category template, but the plugin does not work. I’ve installed the Wordpress 2.8.2, but it still does not work.
Sorry for my English
A few things for me to ask/you to try:
I’m just checking out this plugin, it looks very promising for a problem I’m trying to solve. Very promising.
Before I’ve really used it at all, I noticed a typo in the first line:
Plugin Name: Categorty Templates
Thought you’d like to know!
Ha ha. Thanks! I’ll have to correct that.
Does it work with version 2.7.1?
I have not tested it with any other version of Wordpress other than 2.8, so I don’t know. My guess is it would probably still work, as the methods used in the plugin were not changed from 2.7 to 2.8. If you do install it, let me know if there are any problems. Thanks! ^_^
I’ve installed it and am having a problem. The settings page does not appear in administration (it’s not full, just the title) I can take a screen-cap if u want!
If you could take a screen capture, that would be great.
OK, here’s the image of the error.
Can assign template to gallery, overriding the default category?