How to add "curent-cat" to highlight taxonomy term in single custom post type
-
WordPress doesn’t add “current-cat” class to the “categories” or “taxonomies” list whe in Single Post.
I was able to do the trick for Categories/Single Posts, with this:
// Filter to add the "current-cat" class to categories list in Single Post (https://www.transformationpowertools.com/wordpress/highlight-post-categories-function/2) // -------------------------------------------------------------------- add_filter('wp_list_categories','style_current_cat_single_post', 10, 2); function style_current_cat_single_post($output) { if(is_singular()): global $post; $categories = wp_get_post_categories($post->ID); foreach($categories as $value) { if(preg_match('#item-' . $value . '">#', $output)) { $output = str_replace('item-' . $value . '">', 'item-' . $value . ' current-cat">', $output); } } endif; return $output; }
I’d like to do the same for Taxonomies/Custom Post Types, and I am close with this:
add_filter('wp_list_categories', 'sgr_show_current_cat_on_single', 10, 2); function sgr_show_current_cat_on_single($output, $args) { if (is_single()) : global $post; $terms = get_the_terms($post->ID, $args['shop-categories']); 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); } } endif; return $output; }
But that highlights ALL the terms from the list.
By the way, I get the list from this:
<ul id="sub-nav"> <?php wp_list_categories('title_li=&taxonomy=shop-categories'); ?> </ul>
Anyone knows the trick?
- The topic ‘How to add "curent-cat" to highlight taxonomy term in single custom post type’ is closed to new replies.