Created
January 14, 2015 19:53
-
-
Save jondcampbell/d28cee28f045e3ff7362 to your computer and use it in GitHub Desktop.
add active classes to specific items in the wordpress menu
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
function my_custom_nav_class( $classes, $item ) { | |
// The $item->object_id is the actual post id that the menu $item is referencing. | |
// You could also compare the $item->title but it could change and then the class wouldn't be applied anymore | |
if ( 11 == $item->object_id ) : | |
// Blog | |
if ( is_singular( 'post' ) ) : | |
$classes[] = 'custom-current-class'; | |
endif; | |
elseif ( 7 == $item->object_id ) : | |
// Listings | |
if ( is_page( 232 ) || is_singular( 'listings' ) ) : | |
$classes[] = 'custom-current-class'; | |
endif; | |
elseif ( 5 == $item->object_id ) : | |
// Pioneers(locals) | |
if ( is_singular( 'locals' ) ) : | |
$classes[] = 'custom-current-class'; | |
endif; | |
elseif ( 60 == $item->object_id ) : | |
// Shop | |
if ( is_singular( 'product' ) ) : | |
$classes[] = 'custom-current-class'; | |
endif; | |
endif; | |
return $classes; | |
} | |
add_filter( 'nav_menu_css_class', 'my_custom_nav_class', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment