ControlledChaos
8/8/2016 - 8:49 PM

Mark WordPress menu item parent as active when on custom post type.

Mark WordPress menu item parent as active when on custom post type.

<?php

function ccd_cpt_current_nav_class( $classes, $item ) {
	
	global $post;
	
	$current_post_type = get_post_type_object( get_post_type( $post->ID ) );
	$current_post_type_slug = $current_post_type->rewrite['slug'];
		
	$menu_slug = strtolower( trim( $item->url ) );
	
	if ( strpos( $menu_slug,$current_post_type_slug ) !== false ) {
	
	   $classes[] = 'current-menu-item';
	
	}
	
	return $classes;

}
add_action('nav_menu_css_class', 'ccd_cpt_current_nav_class', 10, 2 );

?>

Custom Post Type Active Menu Item

WordPress Snippet