Skip to content

Instantly share code, notes, and snippets.

@SErr0r
Created June 20, 2016 13:58
Show Gist options
  • Save SErr0r/af0e4aa7b4101a652efbdf0b9f56ee00 to your computer and use it in GitHub Desktop.
Save SErr0r/af0e4aa7b4101a652efbdf0b9f56ee00 to your computer and use it in GitHub Desktop.
Add active class to menu items of current taxonomy
<?php
function current_taxonomy_item($classes, $item) {
global $naventries;
$naventries[$item->ID] = $item;
if($item->type == 'taxonomy') {
global $post;
$terms = get_the_terms($post->ID, $item->object);
$currentTerms = array();
foreach($terms as $term) {
$currentTerms[] = $term->slug;
}
if(is_array($currentTerms) && count($currentTerms) > 0) {
$currentTerm = get_term($item->object_id, $item->object);
if(in_array($currentTerm->slug, $currentTerms)) {
$classes[] = 'current-menu-item';
}
}
}
return $classes;
}
add_filter('nav_menu_css_class' , 'current_taxonomy_item' , 10 , 2);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment