Walker News

Display WordPress Categories In One Comma-separated Line

Can you tweak the wp_list_categories function to display or show WordPress categories in one line without the <br /> tag (line break) or HTML list style? Well, certainly you can get this done in one of the at least 3 ways that I know of.

The first and common way is by using a plugin, if there is one. Second way will be create a function for wp_list_categories hook, via functions.php template file of the active WordPress theme. The last option is to modify the wp_list_categories directly in wp-includes/category-template.php.

Obviously, the last option is highly NOT recommended unless you don’t mind to include the mods in this WordPress core file each time the WordPress upgrade is done.

From performance point of view, I am just hesitating to use yet another plugin if there is a better alternative. So, I opt to write a really simple filter function for this subject.

Since the wp-includes/category-template.php core file calls apply_filters function before echo the $output, this is a chance for calling add_filter to include the mods to fine tune or tweak the categories output.

Portion of PHP code in wp-includes/category-template.php:
$output = apply_filters( 'wp_list_categories', $output );

if ( $echo )
   echo $output;
else
   return $output;

1) Open the functions.php template file of the active WordPress theme. Create this functions.php file with the following template code, if your active WordPress theme does not have one:
<?php
// user-defined functions start in the next blank line:

?>

2) Add this line of code, effectively tell WordPress core engine to include your new function for the hook as the apply_filters of the hook is called – the add_filter add myCatNoBrk function that accept 1 function argument to the wp_list_categories (hook) and explicitly using the default priority of 10:
add_filter('wp_list_categories', 'myCatNoBrk', 10, 1);

3) Next, add the myCatNoBrk function that contains the instruction of how to format / tweak the WordPress categories appearance (i.e. to change line break tag to comma):
function myCatNoBrk($OrgCat) {
   $CatNoBrk = preg_replace('/<br \/>/',',',$OrgCat);
   return $CatNoBrk;
}

4) Save and close the functions.php.

5) Now, find out which of your active theme template files call the wp_list_categories and update the style option to “none”:
wp_list_categories('style=none&title_li=');

Although the “none” style does not show the categories in list style, it stills append line break tag to each of the category shown. Thus, the myCatNoBrk function is called by the apply_filters to remove the line break tag.

Custom Search

  1. Dan 30-08-10@08:51

    Hello, Thank You. This was useful to me. It does however add a comma at the end of the last category listing. This is a good start and I’m sure I’ll be able to figure out how to get PHP to iterate through the list if I want. For now I just replaced the commas with space.

  2. Dianakc 24-10-10@03:51

    Thank you a lot! I was looking for this about weeks.

  3. Evie Milo 06-03-11@03:21

    Is there a better way of doing this so you would end up with a properly coded list ie. with list tags? I’ve tried your solution but taken it out as the li’s are missing.

    I’ve currently got this in my index.php file:

    <?php wp_list_categories('orderby=name&title_li=Select A Category:'); ?>
    

    and want to add comma’s between each category and can’t figure out how to do it. Any ideas?

  4. Walker 06-03-11@16:14

    @Evie, the solution shared in this post expects to tabulate categories in one single line, with each of the categories separated by a comma.

    Therefore, the step-5 explicitly change wp_list_categories to be “style=none&title_li=”, i.e. there will be no li expected.

2017  •  Privacy Policy