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.
Hello Mark! thank you for the plugin.
I have a question. I installed this plugin in my blog (wordpress 1.5.1.3), and I have tried to translate some phrases such as “Notify me of followup comments via e-mail”, or “You are the author of this entry. Manage subscriptions” to Spanish.
I have already edited the file “subscribe-to-comments.php” to change them, but it seems to have no effect in my page, because they’re still in english!
What do I have to edit? do you know about any translation up to date?
Thanks in advance!
Well first… this comment is on the wrong entry (wrong plugin!) Second, you can download a Spanish translation on the Wiki page.
Well I was add to my site
Thanks
Wow thanks for the coolness
I want to thank you for comment # 58 it worked for me, thanks!
Does this work with WP2?
All the best
As far as I know.
This plugin does work for me but contains a major bug.
As soon as this plugin is enabled (I use many others) the admin panel breaks. I can still use admin but every time I click on anything, for example posing or enabling/disabling a plugin the page goes blank and you must use active navigation to make it work (i.e. refreshing is not enough)
I can confirm the scenario described by Nick C.
Remove the blank space after
?>
and the problem should go away. Use the “Download” link and you won’t have this problem.I want to use this in my sidebar…
but i dont have any idea how.
“%3$s” was written by %4$s %5$s and filed under %6$s.’,’fauna’), get_permalink(), get_the_title(), get_the_title(), get_the_time(‘l, d. F Y’), get_the_author_posts_link(), get_the_category_list(‘, ‘)); ?>
what do i have to change?
Hi Mark, is there any way to exclude certain categories with this plugin? I’m assuming not as it’s probably an extension of the_category, which I think doesn’t allow for category exclusion either 🙁 Trying to think of a way to prevent my “Asides” category from showing up.
Weisheng,
Change this:
to this:
Instead of
7
, use your asides category ID. that should do the trick.Brilliant! Thanks a million, Mark! Cheers
how do you create sub categories
Does this plugin work for wordpress 2.0.4 ?
very nice plugin,thanks
I install quickly
Wonderful site, wonderful installs , thanks too
*taps fingers on desk and wonders why nobody is postin*
”Postin” -Southern Verb, to post. lol