$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>theme development - Highlight "Show all" item in wp_list_categories|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

theme development - Highlight "Show all" item in wp_list_categories

matteradmin10PV0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 1

Unfortunately 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 );
Post a comment

comment list (0)

  1. No comments so far