Snippets: Add current-menu-item class to main portfolio, staff, testimonial pages

By default active classes aren't added to the portfolio, staff, testimonials items in the main menu while viewing the singular posts, however, it's very easy to add via the snippet below. This will check if the current menu item is the parent post type page as defined in the post type settings in your WP admin (screenshot) and if so and it's a singular post type page than it will receive the active menu item class.

add_filter( 'nav_menu_css_class', function( $classes = array(), $menu_item = false ) {
	$types = array( 'portfolio', 'staff', 'testimonials' );
	foreach ( $types as $type ) {
		if ( is_singular( $type ) && $menu_item->object_id == wpex_get_mod( $type .'_page' ) ) {
			$classes[] = 'current-menu-item';
		}
	}
    return $classes;
}, 10, 2 );

Snippet Type: PHP

Posted Under: Menu

All PHP snippets should be added via a child theme's functions.php file.