skip to Main Content

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 and if so and it's a singular post type page than it will receive the active menu item class.

/**
 * Add current-menu-item class to main portfolio, staff, testimonial pages.
 *
 * @link https://total.wpexplorer.com/docs/snippets/current-menu-post-type-parent-pages/
 */
add_filter( 'nav_menu_css_class', function( $classes = [], $menu_item = false ) {
	$types = [ 'portfolio', 'staff', 'testimonials' ];
	foreach ( $types as $type ) {
		if ( is_singular( $type ) && $menu_item->object_id == get_theme_mod( $type . '_page' ) ) {
			$classes[] = 'current-menu-item';
		}
	}
	return $classes;
}, 10, 2 );
All PHP snippets should be added via child theme's functions.php file or via a plugin (we recommend Code Snippets)
Back To Top