WordPress: the_category Using Multiple Separators Between Categories

In this tutorial, we are going to demonstrate a method of using multiple separators within 'the_category' tag's $separator string. Specifically, we will be focusing on a separator and a leading prefix for the category name that trails the first category. Sounds a little confusing but it will make sense.

In order to list the post category or categories all that is needed is to include the 'the_category' tag: Wordpress Codex. The $separator string provides a way for us to add a comma (or another separation mark) between the items in a list of categories and then throws in an automatic space in case we forget. It's this automatic spacing that we are really focused on since this space needs to be removed in order to for us to use 'the_category' to it's fullest 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 links to child category only, with link text exhibiting "parent/child" relationship.

Basic Usage Example

The below 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 as a suffix and the code for the same set of categories ( 2Web, 3Articles, and 4Plugins ) may look like the following:

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

Starting with our hash tag to catch the initial number, the below 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.

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

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.

Dale Crum
Dale Crum
Owner / Creative Director at Doc4 Design

Want to Hear More?

Need a cost estimate for a new project? Or just want to know what options are available? Get the answers you need by calling us at (479) 202-8634, or drop us a line using the form.