Skip to content

Instantly share code, notes, and snippets.

@baderj
Created Dec 10, 2013
Embed
What would you like to do?
Wordpress filter to highlight the menu item labeled "blog" if not on static pages. Add the code to functions.php
/* ------------------------------------------------------------------------ */
/* 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);
@lisa83

This comment has been minimized.

Copy link

@lisa83 lisa83 commented Feb 6, 2020

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment