This is probably a confusing title of my question, but it's exactly what's describing my problem best.

I want to add the category-slug as classname to my wp_list_categories() output. I found a really simple function that does exactly that it works perfectly.

add_filter('wp_list_categories', 'add_slug_css_list_categories');

function add_slug_css_list_categories($list) {

    $cats = get_categories();

    foreach($cats as $cat) {
        $find = 'cat-item-' . $cat->term_id . '"';
        $replace = 'category-' . $cat->slug . '"';
        $list = str_replace( $find, $replace, $list );
        $find = 'cat-item-' . $cat->term_id . ' ';
        $replace = 'category-' . $cat->slug . ' ';
        $list = str_replace( $find, $replace, $list );*/

    return $list;

So now I have class-categoryslug in my lis for wp_list_categories()

I have just one more little tweak to add to it.

I wrote a function to use wp_list_categories() also to list my taxonomy terms for a hierarchical taxonomy and a custom-post-type … looks like this.

function wr_list_taxonomy($taxonomy, $orderby, $hierarchical) {
    $show_count   = 0;
    $pad_counts   = 0;
    $title        = '';

    $args = array(
      'taxonomy'     => $taxonomy,
      'orderby'      => $orderby,
      'show_count'   => $show_count,
      'pad_counts'   => $pad_counts,
      'hierarchical' => $hierarchical,
      'title_li'     => $title

    return wp_list_categories( $args );

So I can use wr_list_taxonomy() and all my categories are listed.

I want to have the same thing for my taxonomy terms as well, so that the classnames have the slug of the taxonomy term associated with it.

This would be easy because I only have to replace $cats = get_categories(); with $cats = get_terms('event_type');

However I can only do either or. So either I choose to use $cats = get_categories(); and all my normal categories for the normal blog posts have the category-slug as classname or I use $cats = get_terms('event_type'); and all my taxonomy terms have the term-slug as classname.

I have no idea, how I can determine inside the function add_slug_css_list_categories() if the function is currently fired for normal categories or for my tax-terms.

I thought of

add_filter('wp_list_categories', 'add_slug_css_list_categories');

function add_slug_css_list_categories($list) {

    //$cats = get_terms('event_type');
    $cats = get_categories();
    //if ( empty( $cats ) )
    //  $cats = get_categories();

But that doesn't work. Any ideas?

1 Answer 1


Wordpress do:

apply_filters( 'wp_list_categories', $output, $args );

You can do:

function add_slug_css_list_categories($list,$args) { }

Then you can use $args to determine what kind of list you have.

Not sure if you must do that but when you add the filter there is also a variable for accepted arguments:

add_filter('wp_list_categories', 'add_slug_css_list_categories',10,2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.