/**
* Determine if Custom Post Type
* usage: if ( is_this_a_custom_post_type() )
*
* References/Modified from:
* @link https://codex.wordpress.org/Function_Reference/get_post_types
* @link http://wordpress.stackexchange.com/users/73/toscho <== love this person!
* @link http://wordpress.stackexchange.com/a/95906/64742
*/
function is_this_a_custom_post_type( $post = NULL ) {
$all_custom_post_types = get_post_types( array ( '_builtin' => false ) );
//* there are no custom post types
if ( empty ( $all_custom_post_types ) ) return false;
$custom_types = array_keys( $all_custom_post_types );
$current_post_type = get_post_type( $post );
//* could not detect current type
if ( ! $current_post_type )
return false;
return in_array( $current_post_type, $custom_types );
}
add_filter( 'nav_menu_css_class', 'yourprefix_if_search_or_cpt_remove_current_page_parent_on_blog_page_link', 10, 3 );
/**
* Remove blog menu link class 'current_page_parent' when on an unrelated CPT
* or search results page
* dep: is_this_a_custom_post_type() function
* modified from: https://gist.github.com/ajithrn/1f059b2201d66f647b69
*/
function yourprefix_if_search_or_cpt_remove_current_page_parent_on_blog_page_link( $classes, $item, $args ) {
if( is_this_a_custom_post_type() || is_search() ) :
$blog_page_id = intval( get_option('page_for_posts') );
if( $blog_page_id != 0 && $item->object_id == $blog_page_id ) :
unset( $classes[array_search( 'current_page_parent', $classes )] );
endif;
endif;
return $classes;
}