Twitter
Tutorial: WordPress
22. November 2009 · Author: Dale Crum
2 Comments

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:  http://codex.wordpress.org/Template_Tags/the_category. The $separator string provides a way for us to add a comma (or other 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 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 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 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.

2 Comments

  1. Thanks… I was actually looking for a fix to a problem I was having but this was a good find in my search. :) Modifying WordPress built-in functions is a daunting task!

  2. Thanx a lot )

Leave A Comment

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

Multicons is a multi-favicon code generator which automatically inserts the necessary meta tags for both favicons (site-wide and/or admin) and Apple Touch / iPhone icons. Not sure...

IE6 Upgrade Option utilizes the 25K script created by Free the Foxes: http://www.freethefoxes.com/ as a WordPress plugin. Originally this plugin utilized a smaller 7K script but it's...

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 (...

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...