I am new to Wordpress. My version is WP 3.4.2 The site is on my localhost, sorry I cannot share it.
How do I dynamically highlight a nav menu item based on current post? This question appears here on Stack Overflow quite a bit, but I have not found any accepted answers. Some answers I have seen here use Javascript but I cannot because 20% Of my visitors don't have js.
I found a solution, using filter hooks ( a new concept to me ). I have added the filter_hook below, in header.php.
This hook fires on the correct posts, but the final result is wrong. Instead of adding a class name "current-menu-item" to 1 item in my menu, My whole menu is just the string:"current-menu-item";
Can anyone please help me understand what I have done wrong?
if(($post->post_type) =="communities")
add_filter('wp_nav_menu' , 'special_nav_class' , 10 , 2);
function special_nav_class( $item){
if(true){
$class = "current-menu-item";
}
return $class;
}