WP plugin: Post Templates by Category
- plugin:
- Post Templates by Category
- version:
- R1.3
- download:
- post-templates-by-cat.zip
- source:
- post-templates-by-cat.php
Post Templates by Category (PTbC) is another WordPress plugin I’ve had stuck between couch cushions the last several months. The benefit of PTbC is that it lets you set custom templates for all posts within a category.
To install, here are the memorable steps: Download the zip file; extract post-templates-by-cat.php; upload this to your wp-content/plugins directory; and (finally!) activate Post Templates by Category under Plugins in WordPress.
The single variable — actually, it’s a PHP constant — you need to know about is SZUB_TEMPLATENAME_PREFIX. Look in the plugin for this section:
/* >> Begin user-configurable variable >> */
define('SZUB_TEMPLATENAME_PREFIX', 'single-cat');
/* < < End user-configurable variable << */
The value for this informs PTbC of the filename, or rather the prefix used for your templates. The default shown above is 'single-cat'. So if you wanted to apply a template to all posts in category #10, you’d make sure to name the template single-cat-10.php.
Let’s say you set SZUB_TEMPLATENAME_PREFIX to 'category':
define('SZUB_TEMPLATENAME_PREFIX', 'category');
A benefit of this is that it causes the template used for a specific category query (something built into WordPress since version 1.5) to be “annexed” by posts within that category. So a template for category #5 (category-5.php) will also be applied to all posts in that category.
Whatever scheme you go with, just make sure to end each template with -#.php, replacing # with the numeric category ID. Putting PTbC to use merely requires creating the template(s). And for help along those lines check out the WordPress Codex articles on Templates and Themes.
$template_name variable with the SZUB_TEMPLATENAME_PREFIX constant. Information above was edited to reflect the change.
Author: Kaf Oseo
Categories: WordPress
Comments: (93) · Leave a comment · Comments RSS2 · Trackback URL
I’ve been using the .2 version of this plugin on my site for quite some time– it’s great. Is there something new with this version?
BTW, any progress with download manager?
Getting out R1 was less about new stuff than streamlining the code. One real difference is the category ID array was removed; PTbC just assumes there’s a template, returning nothing if there’s not. This was to simplify from a user’s perpective, letting you drop new templates in without editing the plugin.
I’ve actually set aside most of the upcoming weekend to work on Download Manager, and I’m hoping–as long as other things stay out of the way–to get the final beta out some time next week. Just keep in mind that programming and time management don’t go well together. Especially for me.
Ok, so I have no pressing need to update then, from what I gather, as r1 is working fine and why tempt fate? :)
er .2 of course
Found a flaw in the plugin. If you do not have a template for all your categories it will load the index page. Now I don’t know if this is an issue with the default template, but my index template does not display a post. I had to add these lines to the plugin:
// AFTER
if(file_exists($template))
{
return $template;
}
// ADD
else
{
return TEMPLATEPATH . ‘/’ . ’single.php’;
}
Thanks for the great plugin. Works like a charm - especially with the above mentioned fix.
Ryan
Hi, I’m having a bit of trouble with the plugin. It’s already active, I’ve changed the variable to ‘category’ and uploaded to the theme folder ‘category-XX.php’, but still the new category design doesn’t work.. am I missing anything?
Thanks!
Sorry Marya, I’m not sure of the problem. I’ve tested this several ways, and if one alters $template_name to ‘category’ and names a template ‘category-XX.php’, XX being a numeric category ID, it should work.
The only thing I can suggest for now is to make sure the theme directory you upload your template to is the one for the *active* theme. Beyond that, it would help to know if you made any modifications to the theme, or if you’re running any template-related plugins—besides this one, that is.
Thank you for answering! There was a little mistake, but now everything’s working fine! :) Great plugin!
This is one fantastic plugin !
If you ever feel the need to update/extend it, an option to chose the prefered template if a post belongs to several categories would be great. (it takes the template for the last category (highest ID) now)
This is a great plugin–however, I was wondering if it could be tweaked to work for authors instead of categories. I tried having a go at it myself, but I’m just not knowledgeable enough. I suspect it would be a fairly simple thing to do. Is that true?
Great work on the plugin as it is, though! It’s seriously helped me out a lot–thanks for it!
ThePete, depends on your meaning of “tweaked.” It’s possible the plugin could be rewritten to pass templates to posts based on a specific author, but at present there’s no analogous feature in WordPress for unique author templates as there is for categories.
Oh. In other words, it’s not as easy to do it for authors as it was for categories? Oh well, just thought I’d ask since your plugin works so well. My idea was to have multiple bloggers at my site but have them each get their own layout. I can do it for author compilation pages (as in, all of the posts for a given author) but when you click on a specific post you find yourself back with the regular layout.
Excellent plugin, that deserves a donation! I’m gonna make it now :)
Hello there,
I’ve used the plug-in for a couple of sites, but never in conjunction with category templates (of the form ‘category-#.php’). It worked fine.
The current site I am developing uses ‘category.php’, ‘category-#’ and (I hope) ’single-cat-#’ templates (the latter using your plug-in.
However, the problem described by Aeos (above) has reared its ugly head.
Question: Has Aeos’ recommended update been included into the latest version of the supplied plug-in? Examining the code, there does not seem to be a line in which directly correlates with the recommended change.
Many thanks…
Robert, it’s there. Look for the
if(!$template)bit towards the end before $template is finally returned.Hi robert.. big plugin.. I have installed on my blog…
but is possible modify or exist a similar plugin for different template of archive page of each category?
Thz a lot…
Question:
Will this plug in allow an admin to set up an entier new “theme” (complete with new header, etc.) for each category or just reformat the sidebar and content sections?
Thanks!
Chaz, one *could* use a template that in effect displays a completely new look, or theme. But that would be on the shoulders of the template designer.
brilliant plugin!
It works almost perfectly for me- but, whenever it’s activated, all of the admin pages display warnings at the very top:
“Warning: Cannot modify header information - headers already sent by (output started at [my directory]) in [my directory] on line 10″
It displays the same warning four times, for lines 10, 11, 12, and 13. I can deal with this, but ideally I’d prefer it not happen, haha. (I’m running Wordpress 1.5– does that mess it up?)
Thanks, it’s an awesome piece of work.
I tried it at home on my apache webserver - I’ve changed the variable to ‘category’ and uploaded to the theme folder ‘category-2.php’, but still the new category design doesn’t work.. same problem as Marya. My theme is selfmade, based on default-theme. With default theme it runs the new template - what does the plugin need to run, what ist not to be changes in the theme??? (My theme has another header and a second sidebar) ?
Greetings
Claudia from Berlin
Andrew - I had the same problem and figured out that it was simple to fix by editing out any extra whitespace at the beginning and end of the plugin file. See here for more info.
I’m using this whith wordpress 2.0 without any problem!
you’re amazing. one day i want to grow up to be like you. thanks for being born and all that! if you ever feel like nobody appreciates your work, remember that at least one person out there does.
This seems to have a lot of potential. Any idea if it will be incorporated into future WP releases? Your plugins are so useful and simple.
One quick question, if you tag your posts with more than one category, which category does this plugin default to? I’m assuming the first one listed? Thanks for a great set of plugins.
It works perfectly except that it breaks wordpress 2.0.4. Basically, if you make a post or edit anything, it can’t redirect to the next page. When the plugin is activated, problems, once it is deactivated, no problem. I sure wish I could get the bug fix.
Dag blummit, whitespace man, whitespace man. :) “Bug” is fixed, operator error. :)
Great plugin and thanks a million.
for the page not the articles is the same or not?
unless i’m misreading the description of the plugin, i think wp 2 does this out-of-the-box. simply save your category template as
category-#.phpwhere the # is the category number. that template is then called whenever a post in that category is displayed. works for me in 2.0.4.Hello!
This plugins seems to be exactly what I want, but I am missing something… I can’t cofigure it.
I have this category, “Receitas”, with some children-categories. I want their posts have a different template, so I wrote:
$category = ‘category-12.php’ but it didn’t work.
I wrote, also:
$receitas = ‘category-12.php’, and nothing happened. I tried, really, dozens of formulas… :(
Please, please, please, can you help me? I know it’s my fault, but I’m completely lost with these tags!
Btw, I’m using “Categories Template Inheritor”, GREAT plugin, thanks!!!
Greetings,
Luciana
Brasil
OMG…
I was thinking it was so much complicated that it really is! When I tried the simpliest way, it worked - obviously. Anyway, thank you for the wonderful plugins!!!
I am fairly new to WP and would like to ask if you made something similar for Pages? Instead of categories, it would depend on the root page. Thanks.
I wish to create a mini blog on a Page using a single category. Then I wish to post regularly and have the posts only appear on that Page. Does your plugin do this?
Do I create a new template file for the single category? Can I copy the page.php file and change its name to single-cat-xx.php? Do I leave it blank? Not sure how this is done. Please advise.
David
Thank you for the plugin work here.
I have a question. When using this plugin, if a post is in multiple categories, which template does it inherit? When I delete single.php in the template folder, it inherits index.php. I got this plugin to work for my category pages, but not for posts.
Hi, you may be too busy to get back to me, but my .php skills are so minimal, I am hesitant to begin ‘hacking’ without first asking 2 quick questions. 1. Are themes in this instance (someone mentioned posts accepting new headers, footers, content) set up like .css (are they saved as extra .css files in the wp-content/themes/xxx folder?); and 2. Is the .php code allowed, ‘category-2.php’,the 4 line code, is this just repeated within the same file, for each category (i.e, say 1-4), and the program knows to locate the corr. theme within the themes folder and apply it to a particular post, that many times over? (Personally I don’t see how the text will show up, but then I haven’t tried implementing this yet). Maybe you will let me know if I am on the right track. thans, cf
Hi, looks like a great plugin but I’m wondering… is there a way to apply the new template to ALL categories. Basically what I’m trying to do is have all my posts use a different template than my main page. Thanks in advance.
I think the basic WordPress installation already caters for that, Chris. Can you not use the template ’single.php’ ?
What is the difference of this plugin compared with this? They seem to do the same thing to me. What is the advantage of this one? Thank you for any clarification.
Carly - This one styles on a per category basis. I’ve found it particularly useful when I wanted many post/articles to have the same style, but still maintain a distinction between different categories. The Boren.nu plug-in is useful if only a few posts need to have an idiosyncratic style… but if you have a high frequency of posts it might mean a lot of work, styling them individually. This template provides a welcome mid-point between thge plug-in you mention, and the templates provided with the default WordPress installation.
Hi there
This looks like an excellent plugin. Good work… but I wonder if you can help.
I was looking for a method of applying a style to posts in a category and any posts within children categories of that particular category - does this plugin have this capability??
To clarify if I have posts in category Y which is a child of category X and create a page called single-cat-X is there any way of getting that page template applied to posts in category Y also? And any other children categories that I were to create in the future.
Thanks
Matt
Just a heads up that the latest version as linked above breaks wp-email. They both try to set cookies, with post-templates-by-cat.php coming in first, thus preventing wp-email from doing what it needs in order to run.
R1.2 does NOT have that issue, though.
hi there ! it’s a great plugin !
But I have active the plugins and edit the the php to : define(’SZUB_TEMPLATENAME_PREFIX’, ‘category’) , and renamed a theme to “category-3.php” , it’s not working, I’m using WP 2.1 , anything that i have to do to make it works ? Thanks alot !
I’ve not been able to make this plug-in work. What I want to do is have two separate templates … one a regular index for the main page of the blog, and another template that displays a different layout, for a second blog page.
I’ve tried renaming my index file to category-#.php and also tried renaming it to single-cat-#.php, as defined in your notes, but neither of these seem to work. The plug-in is activated and I followed the other steps in the documentation. Is there some other step involved - ? What do I need to name my alternative layout pages to allow this plug-in to see them - and where should they be placed - does category-#.php belong in the same root directory with the index.php page? thanks for help
I am wanting to upgrade my wordpress to the new version. I rely alot on the Category Template Inheritor plugin and the Post template by catigory plugin. I am wondering a few things.
First, is Category Template Inheritor and Post template by catigory wordpress 2.1 compatible.
Second, Does the 1.3 upgrade of Post template by catigory make the Category Template Inheritor obsolete or do I still need them both.
Thanks
Jeff
Please forgive my ignorance as my PHP and WP Template knowledge is sketchy. I need a way to have all posts in a particular category have a particular look regardless of what page they appear on. I’ve installed this plug in but as installed it simply changes the “category page” and not the actual post.
Can I do use this plugin to customize the all posts within a category but not the category page?
Thanks for the great plug-in! Its working like a charm!!
Would’ve been in trouble without it…
Best,
ben
I have a website that is using on older plugin that you made. It was the same as this one but it did it for the children of the parent, not the post of the cat, I think the plugin was called Category template inheritor. I have it an older wordpress and love it, I my new wordpress is 2.1 and it does not scheme to be working for me quite right. If it works in 2.1 can you help me with getting it to work. But I love the plugin and would love to be able to use it. I have version 0.1, also does post-templates-by-category work with 2.1 as well.
Thanks
Jeffrey Penner
http://www.mycampresources.com
Hi
Does the plugin work with 2.1.2 yet please? I am getting a blank page if I dump the code in.
Thanks
Rich
I tried this on 2.1.1 and it works perfectly. Nice work. Are there any plans to extend it so that it also applies to any all categories of single-cat-*?
I’m using this plugin, and it seems to pick up the template correctly (sidebar or no sidebar, etc) but it loses my background image. It’s just blank. Any ideas?
thanks a lot for this great plugin. I been trying to figure how to add ratings to only posts belonging to a specific category. Thanks to this plugin it now works just as i want it. you can check it out at http://www.hostingreviewdirect.org
Would this work for the index like index-cat-10.php? If we just put single-cat would it recognize index-cat, you know? So the archives off of index for the category follow a certain layout? Other people have asked this, but I don’t think responders understood enough to comment. :-\
Looking through, I don’t think the feature is built-in, but I could probably add it myself. :-x
Ahh, apparently a plugin is not necessary to show separate category templates for the archives:
http://codex.wordpress.org/Category_Templates
So we can happily use this plugin and let WP do what it does for the archives. :-) I’m about to try it.
Ahh, it doesn’t support child categories following the template. Perhaps we do need a nice little addition to your plugin? I couldn’t do it, I tried. Not sure if I try some more could I even make children categories work like that.
I think i’m doing something wrong…
I have a category with the id 3. So i want post in it, when sinle, to show in a certain way. I created a template single-cat-3.php and uploaded it in the theme folder. I modified the plugin like this: define(’SZUB_TEMPLATENAME_PREFIX’, ’single-cat-3.php’); . Doesn’t work for me. :(
I would also like to know if is posible assigning to each category a template to display single post.
Ty.
Resolved!
Plugin work, but don’t load content under post like comments box and so on.
thanxs
Hello,
Please, could someone give a more detailed description as to how to set-up this plugin. What other files must be created, and where are they to go?
I would like to use a different sidebar (I have two sidebar files - sidebar and leftsidebar) for each category (I have three categories)? Could you please use sidebars as your example for your explanation?
Do I have to add a category file into my theme directory (where none exists)? And where do I reference use of a different sidebar? Do I have to create sidebar-1.php and leftsidebar-1.php, category-1.php, index-1.php?
OR… is this plugin not what I assume?
One good step-by-step installation description would allow any novice (such as myself) to more easily follow the process - Then this plugin would be a real asset to Wordpress - Then Wordpress ‘Blog’ could then be used as a normal Website. But as it stands now, you need to be a genius (which most of you seem to be) to be able to use Wordpress as a normal Website, such as to allow different sidebars for different categories or pages etc.
Any help would be greatly appreciated. Thanks!
Sincerely,
Clayten
Victory, or anyone,
How did you resolve the single post issue? I’m not getting the custom look to show up with single posts.
also, I’m trying to figure out how to have different looks for different categoris, not just one category. How to ocnfigure? I get error message in all the ways I try.
Same here, It works when I see the category (the list of posts) but when I see the sigle post of that category is still using the general template.
How the template has to be done?
Ok I get it! to have all the sigle posts on that category you have to sue “single-cat” and single-cat-#.php
Fantastic plugin. Thanks Kaf! :)
After hours of configuring I finally got it to work. All post on my Category 18 now displays different layout. Heres my procedure.
Assuming you have uploaded and activated the plugin:
1. Go to Wordpress Admin page
2. Select the “name” of the category you wish to use this template and note on the “ID” which is the category number. In my case its 18.
3. Go to the directory (using FTP) of the template and make a copy of the single.php (or any template you wish to alter). single.php is the template used by the post. In my case I duplicate it to “single-cat-18.php” where 18 is my category ID.
4. Edit the plugin “post-templates-by-cat.php” in the plugin folder and modify the part with
define(’SZUB_TEMPLATENAME_PREFIX’, ’single-cat’); (it didnt work for me if I used ’single-cat-18′ so I leave it that way.)
Im using WordPress Version 2.2.2
Thanks so much, I just want to note that this plugin works with WordPress Mu. Of course, it must be used with caution. :-)
Will this display on the front page of wordpress? Right now im using my own theme and i editing index.php to get the look i wanted. Should i have edited single.php to change the front page and this is where i went wrong?
I dont really care about single pages for the news. Im just intrested in having different formatting on the index. And when i say different formatting i mean like completely different ways of showing up for example one with a div on top of the text div for a screenshot
I keep getting
Warning: Division by zero in halo3news.com/wp-content/plugins/post-templates-by-cat.php on line 62
any ideas?
Also, I’ve added these lines to the plugin, but I am not sure I added them in the correct spot:
/ AFTER
if(file_exists($template))
{
return $template;
}
// ADD
else
{
return TEMPLATEPATH . ‘/’ . ’single.php’;
}
Could someone post EXACTLY where to paste these in. Assume you are explaining this to an 124 year old with severe memory problems…:-P
I have implemented this plugin (thank you!) to create the 9 sub-themes of my site, growandknow.com, and tie them to the various major categories.
However, even with the plugin, I still only get a default “single post” page — not one of the several themes.
I assume that I need to make 9 variations of single.php to accommodate this…? In what files is single.php called? How do I get WP to make any calls for a single-post-page divert to the different themes?
Also, is this plug-in compatible with 2.3? And, will I have to re-install the plugin and its settings after an upgrade??
Sorry, no disrespect, but it seems this board isn’t being monitored?? Kaf responded last in 3/2006, and it is now 10/2007. Just thought I should point it out for anyone else hoping for some answers here. Best wishes to all.
I have tested and verified that this plugin is compatiple with WP 2.3.
I set SZUB_TEMPLATENAME_PREFIX to ‘category’:
define('SZUB_TEMPLATENAME_PREFIX', 'category');I created a template with filename category-74.php.
It doesn’t work. Why not?
Category “sites” is category # 74.
http://www.imagiscape.ca/blog/sites/creativecleanersca
It should appear the way this page (not post) appears:
http://www.imagiscape.ca/blog/a-convenient-truth
(I’m using WordPress 2.3.)
I did everything right (!), but it doesn’t work.
I set the prefix to category
define('SZUB_TEMPLATENAME_PREFIX', 'category');and created a template with filename: category-74.php
It doesn’t work. Why not?
Category 74 is “sites”.
The post http://www.imagiscape.ca/blog/sites/creativecleanersca
should have a layout that appears like that of
http://www.imagiscape.ca/blog/a-convenient-truth
(with the title at the top in the page = the page/post’s title)
I also tried using ’single-cat’:
define('SZUB_TEMPLATENAME_PREFIX', 'single-cat');and filename:
single-cat-74.php
This did not work either.
P.S. http://www.imagiscape.ca/blog/a-convenient-truth is a page, not a post.
Just wondering if this plugin is still good for WordPress version 2.3.1?
Just to let you know I’ve installed your plugin on WordPress 2.3.1, and it worked for me flawlessly. Thanks a million times.
Easy way to do this….
http://lorelle.wordpress.com/2005/09/22/creating-multiple-single-posts-for-different-categories/
worked in two minutes.
Excellent plugin. Worked like a charm for me on version 2.3.2.
I had been using this plugin successfully until our recent upgrade to 2.3.2. Currently the Category templates work, but the single post templates do not. I have everything set up properly. Does anyone have an idea why this would happen upon upgrade to 2.3.2?
My files are named: single-cat-8.php, single-cat-54.php, single-cat-110.php, etc. I am also using the Category Template Inheritor: http://wordpress.org/support/topic/58382
This doesn’t appear to work in 2.5. Uploading it into the plugins directory actually doesn’t make it show in the plugins activation/deactivation area… I haven’t had this happen with any other plugin so far.
Just wanted to tell everyone that this does work in 2.5. Firefox just botched up my download of the script.
I do have a problem with the admin in 2.5. Saving gives a “headers already sent” error.
alright, it’s fixed by removing the space at the end, right after php closes ?>
Thank you so much Kaf. You have saved so many of us such headaches. Thank you for sharing your knowledge with us. Worked perfectly for me with 2.5. Many blessings from Tacoma’s website and graphic designer
i’m sorry Dear Kaf. I’m confussing after activate this post template by Category.
What should i do next?
I don’t know in file what i can write this code in the template path??
/* >> Begin user-configurable variable >> */
define(’SZUB_TEMPLATENAME_PREFIX’, ’single-cat’);
/* < < End user-configurable variable << */
do i have to change ’single-cat’ syntax above??
example i have sport and art in my post-category.
how can I use different tempates in every category of post??
Please Help me using your plugin’s.
After a long study, i can use your plugin,s
Thank’s
Great Plugin’s
I have question about how to show list post from each category in our sidebar?
it’s seem a easy for advanced wordpress programmer’s..but it’s very difficult to me..
Kaf or someone who can help, Please help me…!
Thanks
Ok, I got your plugin installed and now I’m kind of lost. What are the exact steps that I need to do to get this working.
Thanks
Kaf —
Fantastic plugin! I’m using it on Napoleon’s Gambit to handle the Chapter Notes section, which has saved me a lot of duplicated templates for each Chapter. Thanks!
I combined it with your also excellent Category Template Inheritor to make my life even easier. I made a small modification to that plugin and wanted to share it here but couldn’t find a page specifically for it — feel free to roll this back into the actual release if you find it useful. On that site’s Napoleon’s Gambit Chapter Notes section, each chapter’s notes is a subcategory of the general Chapter Notes category (e.g.: Chapter Three is a subcategory of Chapter Notes), so I just defined a template for the parent and the rest inherit it. I needed the ability to override that for the first few items which display a single post instead of the index listing. I modified the plugin so that it first checks to see if a template exists for the requested category before applying the inheritance, enabling you to override for one subcategory by just creating a template for it. Here’s the change:
if( file_exists(TEMPLATEPATH . '/category-' . $category->cat_ID . '.php') )
$template = TEMPLATEPATH . '/category-' . $category->cat_ID . '.php';
elseif( $category->category_parent && file_exists(TEMPLATEPATH . '/category-' . $category->category_parent . '.php') )
$template = TEMPLATEPATH . '/category-' . $category->category_parent . '.php';
elseif( file_exists(TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php') )
$template = TEMPLATEPATH . "/category-" . get_query_var('cat') . '.php';
elseif( file_exists(TEMPLATEPATH . "/category.php") )
$template = TEMPLATEPATH . "/category.php";
(I’m also using the excellent My Category Order plugin for that section so I can control the order in the left sidebar.)
Thanks again!
The plugin works fine.
But I found that I can’t write new posts in the editor in Visual mode, only HTML.
I’ve deactivate all plugins and activated 1 by 1. It was OK. When I activate Post Templates by Category plugin editor stops working.
How to fix it?
Thank you.
This plugin served me very well when I was using WP V2.1 - 2.3. If you are having problems with 2.5 or newer versions of Wordpres with this plugin, you might try Idealien Category Enhancements which takes a slightly different approach to the same problem. It allows you to use different templates per category / post that is managed through the admin interface.
I wanted exactly the same this plugin does, but it does not work i think with 2.7. i tried idealien category plugin also but that is not something i need.. till now i am manually using templates for a category. is there any plugin which can setup a template for all the posts under a category.??
The PI worked kinda fine, but i had to do few changes. I am left with a great work on my post - categories template, but i can’t get rid of this two error messages on top:
Warning: include_once(D:wampwwwpartireper/wp-content/plugins/post-templates-by-cat.php/) [function.include-once]: failed to open stream: Invalid argument in D:wampwwwpartireperwp-settings.php on line 465
Warning: include_once() [function.include]: Failed opening ‘D:wampwwwpartireper/wp-content/plugins/post-templates-by-cat.php/’ for inclusion (include_path=’.;C:php5pear’) in D:wampwwwpartireperwp-settings.php on line 465
They have something to do with:
line 465 = include_once(WP_PLUGIN_DIR . ‘/’ . $plugin);
Any help?
Is this plugin still being updated and supported?