• Skip to primary navigation
  • Skip to main content

Genesis Community

Resources for Genesis WordPress Theme Users

  • Facebook
  • Twitter
  • Slack
  • Wiki
  • Login
You are here: Home / Wikis / WordPress / Remove current_page_parent class from blog page menu link when on unrelated CPT or Search Results page

Remove current_page_parent class from blog page menu link when on unrelated CPT or Search Results page

/**
 * 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;
}

Posted by: christina Category: WordPress

« Add Login/Logout menu items conditionally to Primary nav menu
Add current-menu-item to CPT menu parent when on single entry »

Copyright © 2026 · Genesis Sample on Genesis Framework · WordPress · Log in