Adding a Separator to wp_list_categories

In WordPress, if you’re displaying your categories list inline instead of block, odds are you’re going to want to be able to define some HTML to separate the outputted items. Since wp_list_categories doesn’t include a separator argument, you need to do a simple PHP string replace to add one in.

I didn’t see any answers to this question on the WordPress forums so I thought I’d post the code I wrote here.

<?php
$needle = '</a>';
$separator = '<span class="pipe">&nbsp;|&nbsp</span>'; // fill in your separator here 
$cat_list_args = array(
	'echo'  	=> __( 0 ),  // sends output to variable
	'title_li'	=> __( '' ), // removes title of list (optional)
	'hierarchical'	=> __( 0 ), // makes subcategories not show as inner list items (optional) 
	'exclude'	=> __( 1 ), // excludes uncategorized (optional)
);
$cat_list_arr = wp_list_categories( $cat_list_args );
$cat_list_mod = str_replace($needle,$needle.$separator,$cat_list_arr); // switch to $separator.$needle if you want separator inside a tag
echo $cat_list_mod;
?>

On a related note, if you want to hide the separator of your final list item, CSS pseudo-classes makes that super easy.

nav li:last-child .pipe { display: none; }