WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 taxonomy terms 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 blogposts have the category-slug as classname or I use $cats = get_terms('event_type'); and all my taxonomy terms have the category-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?

share|improve this question
up vote 1 down vote accepted

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);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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