Skip to content

Instantly share code, notes, and snippets.

@jondcampbell
Created January 14, 2015 19:53
Show Gist options
  • Save jondcampbell/d28cee28f045e3ff7362 to your computer and use it in GitHub Desktop.
Save jondcampbell/d28cee28f045e3ff7362 to your computer and use it in GitHub Desktop.
add active classes to specific items in the wordpress menu
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