Comma-Separated Categories in WordPress
Recently I worked on a project in WordPress that was going to have a lot of categories and I wanted to display the categories comma delimited. After fooling with WordPress’s wp_list_categories function to list categories, I realized that it only allowed you to display categories as a list or delimited by a <br>. So after looking around on Google to no avail, I decided to whip up a function that allows for different delimiters. The function is fairly simple with one argument for the string of the delimiter.
↓ Works with WordPress 2.3
↓ Works with WordPress 2.0 > 2.2
Calling the Function
In my theme folder I have a file called functions.php that gets loaded when the theme is loaded. If your theme doesn’t have one you can create one. Copy and paste this function into the functions.php file. Now all you have to do is call the function in the theme template where you want your category links to show up.
<?php echo delimited_cat(); ?>
You can also pass the function an argument and change the default “, ” delimiter to anything you like. The example below would delimit by a pipe with spaces on both sides.
<?php echo delimited_cat(' | '); ?>
Uses this code at your own risk.
→ Andreas @ September 5th, 2007 at 5:06 pm
Hello, first off, great site and great artwork! I really enjoy your sites. However as a fellow webdesigner and wordpress user I´ve found that the best resource for anything wordpress is the codex.wordpress.com site, which shows how to f.ex get comman or and seperated categories. http://codex.wordpress.org/Separating_Categories
However, great work on the plugin and I´m sure it was quite a learning experience!
Cheers, Andreas.
→ Rik Catlow @ September 5th, 2007 at 5:42 pm
Actually the purpose of the “the_category” template tag is not to list all categories. It’s purpose is to list categories for a specific post. Try using it outside of the post loop. It’s doesn’t list categories just the last one in the loop.
→ Richard Carr @ September 25th, 2007 at 8:12 am
Magic :-)
→ Q @ November 27th, 2007 at 9:01 am
Hi,
I have some problems:
1. if ((float)bloginfo(‘version’) > 2.3) echoes my wordpress version
2. some of the posts get listed when i use your function. I know it’s strange, but they do.
(wordpress 2.3)
Thanks
→ Rik Catlow @ December 2nd, 2007 at 2:21 pm
Redownload the WP 2.3 version. I fixed the problems.