jon-c
1/14/2015 - 7:53 PM

add active classes to specific items in the wordpress menu

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 );