Highlight current category in wp_list_categories

All we need is an easy explanation of the problem, so here it is.

I am using wp_list_categories to display all terms of my custom taxonomy 'categories'.

The function is outputting all my terms successfully but the current category is not being highlighted, even though 'current_category' is set to 1 in my arguments.

The function is being outputted on taxonomy-categories.php, following the naming convention of my taxonomy categories.

It is not inside a loop, could that be why?

UPDATE, here is my function:

$args = array(
    'orderby'               => 'term_group',
    'title_li'              =>  NULL,
    'order'                 => 'ASC',
    'hide_empty'            => 1,
    'use_desc_for_title'    => 0,
    'feed'                  => '',
    'hierarchical'          => 1,
    'echo'                  => 1,
    'current_category'      => 1,
    'taxonomy'              => 'categories'
);
echo wp_list_categories($args);

Note: this works on my single-{post-type}.php template, but does not function on my taxonomy-{taxonomy}.php template.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

This will add a current-cat class to any / all categories connected to the post, not jut one.

Add this to functions.php

function tax_cat_active( $output, $args ) {

  if(is_single()){
    global $post;

    $terms = get_the_terms( $post->ID, $args['taxonomy'] );
    foreach( $terms as $term )
        if ( preg_match( '#cat-item-' . $term ->term_id . '#', $output ) )
            $output = str_replace('cat-item-'.$term ->term_id, 'cat-item-'.$term ->term_id . ' current-cat', $output);
  }

  return $output;
}
add_filter( 'wp_list_categories', 'tax_cat_active', 10, 2 );

Method 2

The answer provided by Howdy_McGee produced a PHP notice for me that prevented it from working correctly. I changed $terms = get_the_terms( $post->ID, $args['taxonomy'] ); to $terms = get_the_terms( $post->ID, 'taxonomy' ); I also added a check that the post has terms associated with it to suppress additional PHP warnings if $terms is empty.

function tax_cat_active($output, $args) {
    if (is_single()) {
        global $post;
        $terms = get_the_terms($post->ID, 'category');
        if (!empty($terms)) {
            foreach( $terms as $term )
                if ( preg_match( '#cat-item-' . $term ->term_id . '#', $output ) )
                    $output = str_replace('cat-item-'.$term ->term_id, 'cat-item-'.$term ->term_id . ' current-cat', $output);
        }
    }
    return $output;
}
add_filter('wp_list_categories', 'tax_cat_active', 10, 2); 

Method 3

Actually this functionality is already build in the wp_list_categories function.

Just use the current_category like this:

'current_category' => get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ) ?? null,

Full example:

wp_list_categories(array(
                'child_of'            => 0,
                'current_category' => get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ) ?? null,
                'depth'               => 0,
                'echo'                => 0,
                'exclude'             => '',
                'exclude_tree'        => '',
                'feed'                => '',
                'feed_image'          => '',
                'feed_type'           => '',
                'hide_empty'          => 1,
                'hide_title_if_empty' => false,
                'hierarchical'        => true,
                'order'               => 'ASC',
                'orderby'             => 'name',
                'separator'           => '<br />',
                'show_count'          => 1,
                'show_option_all'     => '',
                'show_option_none'    => __('No categories'),
                'style'               => 'list',
                'taxonomy'            => 'category',
                'title_li'            => '',
                'use_desc_for_title'  => 1,
            ));

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply