doc4design.com - WordPress: the_category Using Multiple Separators Between Categories
22. November 2009 · Author: Dale Crum
1 Comment

WordPress: the_category Using Multiple Separators Between Categories

In this tutorial we are going to demonstrate a way to use multiple separators within the_category tag’s $separator string, specifically focusing on a separator and a leading prefix for the following category.

In order to list the post category ( or categories as the case may be ) all that is needed is to include the the_category tag:  http://codex.wordpress.org/Template_Tags/the_category. The $separator string provides a way add a comma or other separation mark between the items in a longer list of categories and then throws in an automatic space in case we forget. It’s this automatic spacing that we are really after, this needs to be removed in order to use the_category to it’s full extent. Let’s begin by demonstrating the basic usage of the_category and what happens if we include more than one separator.

<?php the_category( '$separator, $parents' ); ?>

$separator: (string) Text or character to display between each category link. The default is to place the links in an unordered list. $parents: (string) How to display links that reside in child (sub) categories. Options are: ‘multiple’ – Display separate links to parent and child categories, exhibiting “parent/child” relationship. ’single’ – Display link to child category only, with link text exhibiting “parent/child” relationship.

Basic Usage Example

<?php the_category(','); ?>

The above code will print a list of categories separated by a comma, like this: Web, Articles, Plugins. Note that we do not manually include a space after the comma separator and this result is great, but what if we wanted to list our categories separated by a comma and add a hash symbol as a prefix to each additional category name. For example our categories may include a number suffix and the code for the categories ( 2Web, 3Articles and 4Plugins ) may look like the following:

#<?php the_category(', #'); ?>

Starting with our hash tag to catch the initial number, the above code prints like this: #2Web, # 3Articles, # 4Plugins. Unfortunately this isn’t quite what we are after due to the fact that WordPress automatically adds a space between each category as a pre-filled separation. What we are looking for is to list the categories without this additional space.

The Alteration

The alteration is a simple, small adjustment to the WordPress core by removing the tag’s separator space. Look for the category-template.php file located within wp-includes. Then on Line 211 ( tested 2.8.4 – 2.8.6 ) locate the following line of code:

$thelist .= $separator . ' ';

and remove space between the apostrophes above:

$thelist .= $separator . '';

Altered Usage Example

With the newly altered code we can again use the_category tag as normal:

#<?php the_category(', #'); ?>

We now get the desired result: #2Web, #3Articles, #4Plugins. Please note that for any additional instances of the_category where a space is required it will need to be added manually.

1 Comment

  1. Thanx a lot )

Leave A Comment

Please wrap any code within the <code> tag to display properly

IE6 Upgrade Option utilizes the 7.9kb script created by mihai.ile: http://code.google.com/p/ie6-upgrade-warning/ as a WordPress plugin. I preferred this script over the many alternatives...

The WordPress Plugin: WordPress Database Backup by Austin Matzko is one of the more intuitive backup plugins currently available and with no stern warnings to scare off the faint of...

While doing our usual run of site updates and code adjustments we ran into a small issue, how to display WordPress bookmarks with both text and images. Utilizing the "Links" tab ( WordPress...

BookMaster is our answer to what we feel is an odd issue with the WordPress wp_list_bookmarks tag. For those that have exercised the use of wp_list_bookmarks you are well aware that...

Limit-Post is one of the better WordPress post content limiters we have come across both in terms of usability and size. Developed by labitacora.net Limit-Post provides excellent control...