I use wp_list_categories
to display category filters on post archives.
I can use current_category
param to set current category - and such item will get addition class current-cat
. I can even omit this param - in such case the current category will come from get_queried_object()
.
But... I want to show "Show all" link in that navigation. All i have to do is to use show_option_all
param.
And here's my problem. How can I highlight this option, when no category is selected?
I use wp_list_categories
to display category filters on post archives.
I can use current_category
param to set current category - and such item will get addition class current-cat
. I can even omit this param - in such case the current category will come from get_queried_object()
.
But... I want to show "Show all" link in that navigation. All i have to do is to use show_option_all
param.
And here's my problem. How can I highlight this option, when no category is selected?
Share Improve this question asked Mar 17, 2019 at 10:16 Krzysiek DróżdżKrzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges1 Answer
Reset to default 1Unfortunately there is no filter allowing to modify the "Show all" link directly or to add any class to this item:
$output .= "<li class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>";
But there is a wp_list_categories
at the end of function that allows you to modify all the output. Here's a filter that will highlight "Show all" option if no current_category
is set and we're not visiting any term from given taxonomy.
function wp_list_categories_highlight_all( $output, $args ) {
if ( array_key_exists( 'show_option_all', $args ) && $args['show_option_all'] ) {
if ( ! array_key_exists( 'current_category', $args ) || $args['current_category'] ) {
if ( is_category() || is_tax() || is_tag() ) {
if ( ! array_key_exists( 'taxonomy', $args ) ) {
$args['taxonomy'] = 'category';
}
$current_term_object = get_queried_object();
if ( $args['taxonomy'] !== $current_term_object->taxonomy ) {
$output = str_replace( "class='cat-item-all'", "class='cat-item-all current-cat'", $output );
}
} else {
$output = str_replace( "class='cat-item-all'", "class='cat-item-all current-cat'", $output );
}
}
}
return $output;
}
add_filter( 'wp_list_categories', 'wp_list_categories_highlight_all', 10, 2 );