Created
June 20, 2016 13:58
-
-
Save SErr0r/af0e4aa7b4101a652efbdf0b9f56ee00 to your computer and use it in GitHub Desktop.
Add active class to menu items of current taxonomy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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