A friend asked if there was a way for WordPress to display the categories under which an entry is filed in the following way:
Single category: Category1
Two categories: Category1 and Category 2
Three categories: Category1, Category2 and Category3
Four categories: Category 1, Category 2, Category 3 and Category 4
And so forth.
The existing the_category()
function has no way of telling how many categories there are, and so can’t do something fancy like inserting the word “and” between the penultimate (second-to-last) and ultimate categories. So I just made a plugin with a new function called the_nice_category()
.
Download
WordPress Nice Categories Plugin (WP 1.5.1 and above)
Instructions
Go ahead and save this text as a file with a “.php” extension, and drop it in your plugin folder. After activating it, you will be able to use a new function in your WordPress template. The function is the_nice_category()
. You will replace the instance of the_category()
with the_nice_category()
in the “posted” section of your main index file.
the_nice_category()
takes two optional parameters. The first is the text string to appear between categories when the category on the right isn’t the last category. This defaults to “, ” (a comma and a space). The second parameter is the text string to appear between the penultimate category, and the last category. This defaults to ” and ” (the word “and” buffered by spaces.)
If you wanted to display your categories like the default, but with double quotes around them, you would use this code:
"<?php the_nice_category(', "', '" and "'); ?>"
Notice that you need to place quotes on either side of the_nice_category()
. This is to put the quotes at the beginning of the first category, and at the end of the last category.
Notes
Also, if you want to determine if there are multiple categories, you can do so by using count(get_the_category())
.
This code will output “Category: ” if there is only one category, and “Categories: ” if there are more than one.
<?php if (count(get_the_category()) > 1) echo 'Categories: '; if (count(get_the_category()) === 1) echo 'Category: '; ?>
Versions
- version 1.5.1, updated for WordPress 1.5.1 compatibility.
- version 1.5, updated for WordPress 1.5 compatibility.
- version 1.1, merged in updated WP 1.3 the_category() code (only change is that it won’t throw out an error if there are no categories for that entry). Should work fine with WordPress 1.2 as well.
- version 1.0, the initial release.
BB Iverson says
Is there any way to display only subcategories with this? I don’t want to display the parent categories.
Mark says
It doesn’t show parent categories. It only shows subcategories.
BB Iverson says
I’m sorry. I didn’t clarify myself. If I have both the parent category ticked and the subcategory ticked, is there a way to only show the subcategory name and not the parent name?
The reason I ask this is that I have all posts categorized with subcategories, but I want the parent categories to show a total of them in my post count on my category list. The only way of doing that was to tick both the parent and the subcategory.
FYI, I just noticed you have a tick box that doesn’t move down with your “Comment Preview” field, when it moves down to accomodate lengthy comments (like this one). It is the “Do not save my info” tick box. You might want to take a look at that. I’m using IE to view this.
bryan says
Hey I got it to work 🙂
BB Iverson says
Something else I found, in trying to edit all of my posts…
Choosing “All” from the Show Posts From Category dropdown produced no posts, while choosing “None” from that same dropdown produced all of my posts. Shouldn’t it be the other way around?
BTW, what is “None” for anyway? Is it possible to have a post that is not assigned to a category with WP?
Mark says
#5: Thanks for the bugreport. IE was doing something funky with the JS. Fixed now.
#7: What’s the Show Posts From Category dropdown? I’m assuming it’s a plugin you’re using. And yes, that does sound strange acting as it does. As far as I can tell, it is impossible to have an entry that is not in a category (defaults to “General”).
BB Iverson says
Oh no! I can’t believe I posted that comment – to the wrong website! I was trying to get the Batch Category Editing For WordPress hack to work, so I could get rid of the posts’ parent categories. That was so that I could clean up the category displays for each post, while using this plugin. Sorry for the confusion.
Unfortunately, I made another mistake. When I removed the parent categories for posts, the subcategory posts dropped out of the category dropdown on my index.php file. I had to put them all back to where they were.
So, I’m between a rock and a hard place. I don’t want the parent categories displayed under the heading of every post, just the subcategories. But, if I get unassign them from the posts, they won’t appear in the category dropdown.
Is there any other way?
Mark says
Yeah. Upgrade to WordPress 1.3 The function that gets the categories is broken in 1.2 I got it to work by doing a “selective upgrade” of that function.
See here: mosquito.wordrpress.org report
BB Iverson says
“selective upgrade”Â?
Mark says
Basically just upgrading that function. So while your WP install would still be technically 1.2, it’d have the upgraded 1.3 version of that function. You can’t always upgrade a function without adding other functions, but a lot of the time, it is a self-contained upgrade.
BB Iverson says
How did you do that? The URL in that comment, #10, is broken.
Anthony says
Beautiful plugin. Installed in seconds and worked like a charm first time out.
Sadish says
I got a problem after installing this plugin. It shows up fine. but when i click on login, it show, headers are already sent from ‘nicecategories.php’
do u face this problem any time ?
Mark says
Sadish,
This is a generic error, and not really one related to my plugin. The problem is that plugins are loaded before headers are sent to the browser. If you accidentally leave a space before the beginning
<?php
or after the ending?>
, the space gets sent to the browser (normal html). But in order to send that space to the browser, headers need to be sent first, so the browser knows what it’s getting. Then, when WordPress tries to send its custom headers, after the plugins have been processed, it gets an error, because headers have already been sent.So the solution to this problem is to open up
nicecategories.php
and to eliminate any spaces before the beginning<?php
or after the ending?>
. Space inbetween the two do not matter, because these are absorbed by the PHP processor, and are not sent to the browser.Michael Heilemann says
Will you update this plugin for WordPress 1.3? I am in the progress of converting Binary Bonsai to 1.3, and I love this little plugin. Unfortunately it seems to break in the foreach loop under 1.3.
Mark says
Thanks for the heads-up. I’ll take a look at it tonight.
Mark says
Updated it. Tested in WP 1.3
The only thing that changed is that it now checks to see if there is no category, and calls it “Uncategorized” if so (which is what the_category() does in WP 1.3 now). Just redownload it using the link above, and you’ll be good to go.
Michael Heilemann says
When I view my admin, I get the following problem, printed out at the very top of the page:
Parse error: parse error in /Library/WebServer/Documents/binarybonsai13/wp-content/plugins/nicecategories.php on line 24
Michael Heilemann says
For whatever reason, Safari has a problem copying PHP code. I opened Camino instead and copied it. Now it works fine 🙂
CarLBanks says
Binary Bonsai was who made me like this plugin. It is one of the nicest plugins avaliable. Thank you for creating this plugin.
Alderete says
I’m a little confused by the line right after the foreach loop:
$category->cat_name = $category->cat_name;
That looks like it’s assigning cat_name back to itself, a null operation, rather than doing something useful. Am I missing something here? Is this required for WP1.3 compatibility?
Mark says
Alderete, it looks strange to me too… but it exists in the WP
the_category()
function, so I included it. It may be an issue with variable scope. I’m honestly clueless as to its purpose or lack thereof. Feel free to comment the line out and see what happens. 🙂Anibol says
I have a problem with your plugin. If I activate it, then, when i do a login to wordpress i receive a blank page. I have to access to phpAdmin and change the register of wp_options table to de-activate your plugin to make wordpress login work. My version is 1.2.2 Mingus.
Any solution?
Thanks
o-juice says
Hello.
I had done a search for a plugin or hack like this previously, though obviously not thoroughly enough since I didn’t find this site until lbessant pointed me to it from the thread I started on WordPress’s support forum.
Since your plugin was made ages before mine, I’m ceasing development for mine, but feel free to use any of its code if you want to.
Anibol: As far as I know, my plugin works fine on 1.2.2 Mingus which is the install I run on my blog and off-line. I’m too much of a wuss to be on the bleeding edge with nightly builds. ;-P
Stick around though and I’m sure Mr. Jaquith will help you out in adapting his to work on your install. 🙂
Mark says
Anibol, that error is likely due to white space before the first
<?php
or after the ending?>
Make sure that those opening and ending PHP tags are the first and last things in the file you create!
Brad says
When I upgraded to WP 1.5, the category links broke. Instead of pointing to the category, it points to the category base (in my case /archives/categories).
Any suggestions on how to fix this?
Mark says
Brad,
Thanks for pointing this out! Looks like they changed the
function right before 1.5’s release. I’ve posted a 1.5 compatible version.get_the_category()get_category_link()Morten says
I run Wp 1.5. When I activate the plugin I get this printet at the top of my admin header:
Whats going on? 🙂 I chekked to se if the code was correctly copy/pasted – it was.
Mark says
Morten, see comment #30. You likely have a space after the ending
?>
That should be the last thing in the file… any spaces after it will give that error you described.
Brendan says
Looks like the plugin is broken with the WP 1.5.1 nightlies – it’s doing the same thing it did pre 1.5 (showing the catagory base only).
Mark says
Thanks Brendan. I really wish that they’d stop changing the
get_category_link()
function so much. Try the new 1.5.1 version.Brendan says
It would appear the link is dead.
Oops 🙂
Mark says
Doh! It’s good now.
Brendan says
Excellent, indeed it is. Categories are displaying nicely now. Nice work. 🙂
orin says
I liked your plugin and was using it quite happily until I upgraded to 1.5.1. The categories still displayed correctly, but instead of linking to the category archive they linked into the base category structure.
Clarification: what I mean is that if my category permalink was like this: “dogblog.com/category/cute-puppies/”, the plugin was linking like this: “dogblog.com/category/”. This resulted in a not found error page for any category link clicked.
Wanted to bring this to your attention so anyone who enjoyed using this plugin in 1.5 can continue to use it in 1.5.1 (like me!)
Mark says
Orin, there has been a 1.5.1 compatible version available for a few weeks (see above). It sounds like you’re still using the 1.5 version. (I know, it’s a pain that I keep having to release new versions… they keep changing the underlying code on me. I have a new version coming out soon that should be much more future-proof, as well as work without messing with templates.)
orin says
Eep!
Absoulutely correct, sir. Shouldna opened my big mouth when there was a solution right before my eyes.
mike says
@mark-
i recently downloaded your plugin and received header errors (i checked-no white spaces) when activating the plugin. Odd thing is I received the same error with the heatmap plugin as well. It turns out that downloading the file .phps format threw a bunch of junk into the code as to render it unusable. Of course I can’t say for sure if it is the same problem but since I would like to get this to work could you possibly check this out? Thanks! here’s his comments on it…
garblejunk
Mark says
Mike, you shouldn’t be downloading the phps file, but viewing it in a web browser and then copy-pasting the text, making sure there is no leading or lagging whitespace. The colorization is indeed done with html markup, but you’ll only get that if you save the file itself or view its source and save that. You should click the link, select all, copy, paste into plain text program, eliminate leading/lagging whitespace, and save with .php extension, and upload as ASCII to your
/plugins/
directorymike says
sorry mark, that is exactly how i did the first 5 or 6 times and just to make sure, i did it again with same results. i simply copy and paste from browser into notepad, wordpad, dreamweaver (i’ve tried a few) and i just can’t get it to act nice. this is very weird. i’ve looked at the file up and down and can’t see what the problem could be. i can honestly say that with the other plugin when he changed it from a phps file to a txt file for downloading the problem was solved with that one…
oh well, thanks for the reply.
mike says
oops! i know you’re a stickler for spelling and i didn’t want to offend!
:redface:
Mark says
Mike, try this zipped version. There are two versions in there that use different approaches. One is nice because it doesn’t require modification of templates, and the other is nice because it offers better customization. I’m going to merge them together when I have some time, but until then, have at it.
mike says
as of this time the link seems to be dead…
Mark says
Gah! It works now.
mike says
got it! thanks1
i’m having problems ftping into my site but i’ll let you know as soon as i can get in.
mike says
and now my site has nice categories!
thanks for all the help mark!
Sheana says
Not much to add, but damn fine plug-in. Works flawlessly and really contributes to the overall design.
Barry Price says
Hmm, I just get a
500 Internal Server Error
, when I click on any of the plugin links. 🙁Mark says
Barry Price,
Artifact of my server move. It has been fixed since.
Ian says
Thanks for the update…I just noticed it wasn’t working correctly under 1.5.1.3 a day or so ago…’preciate it!