Add filter to wp_list_categories and query what type of taxonomy-terms it use?

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

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?

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?

Share Improve this question edited Jan 30, 2020 at 9:20 Viktor Borítás 3042 silver badges11 bronze badges asked Apr 20, 2012 at 8:10 mathiregistermathiregister 1,54313 gold badges55 silver badges78 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744790414a4593879.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信