0

I'm trying to add an argument to wp_list_categories of the Categories Widget:

function cat_list_show_all( $list, $args ) {
    $args['show_option_all'] = __( 'All Cateogries', 'textDomain' ) . '<span>' . wp_count_posts()->publish . '</span>';
    return $args;
}
add_filter( 'wp_list_categories', 'cat_list_show_all', 10, 2 );

But, it return "Array" on front-end. What's the right way to do it?

0

The documentation for the wp_list_categories filter says that it:

Filters the HTML output of a taxonomy list.

So it filters the final HTML, not the args, but you're returning $args, which means that the final HTML is replaced with the args array. That's why you're seeing "Array".

To filter the args for the categories list in the Categories widget, you need to use the widget_categories_args filter.

function cat_list_show_all( $args ) {
    $args['show_option_all'] = __( 'All Cateogries', 'textDomain' ) . '<span>' . wp_count_posts()->publish . '</span>';
    return $args;
}
add_filter( 'widget_categories_args', 'cat_list_show_all' );

However, be aware that this filter only works for the "legacy" Categories widget. Newer sites will only have access to the Categories block. The Categories block does not have an equivalent filter, so filtering the args is not possible.

1
  • Thanks, too bad it doesn't work for the block in gutenberg.
    – Yco
    Oct 13 '21 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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