Created
December 10, 2013 09:13
-
-
Save baderj/7887807 to your computer and use it in GitHub Desktop.
Wordpress filter to highlight the menu item labeled "blog" if not on static pages. Add the code to functions.php
This file contains 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
/* ------------------------------------------------------------------------ */ | |
/* highlight the menu item labeled "blog" if not on static pages */ | |
function add_custom_class($classes=array(), $menu_item=false) { | |
if ( !is_page() && 'Blog' == $menu_item->title && ! in_array( 'current-menu-item', $classes ) ) { | |
$classes[] = 'current-menu-item'; | |
} | |
return $classes; | |
} | |
add_filter('nav_menu_css_class', 'add_custom_class', 100, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
I came across your code and it is exactly what I have been looking for and works like a charm. Thank you!
However I have got three different pages I would like to have highlighted separately (when on their relevant posts) and I can’t figure out how to do this. My PHP Knowledge is pretty limited as it stands. I’ve tried adding the code multiple times changing 'Blog' to my relevant titles which has made my site crash and when I try and add the other titles into your code, it either highlight all at the same time or creates a Wordpress error. If anybody can help me with this I’d be super grateful. For now I will continue trying;) Thanks!