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.

3 Answers
3

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

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *