Remove ‘current_page_parent’ class from the Blog navigation when on archive pages

Remove the current_page_parent class from the Blog page link when on post and taxonomy archives


On a custom post type archive page or a custom taxonomy archive page you may find the link to the Posts archive page in your navigation (in other words, the Blog page) has a current_page_parent class. If you are using this class to highlight the currently active section in the navigation, you may need to remove this erroneous class from those items to ensure you indicate the correct link.

The code snippet below will remove the current_page_parent class from the navigation link entitled ‘Blog’. Some example conditional statements are included for reference; the snippet will remove the class when the user is on the ‘resource’ post type archive, the ‘type’ taxonomy archive or on the current template is the ‘templates/template-name.php’ file. You can customise these statements according to your needs.

/**
 * Remove current_page_parent class from Blog link
 */

function bb_remove_nav_classes( $classes, $item ) {
    if ( 
        (
            is_post_type_archive( 'resource' )
            || is_tax( 'type' )
            || is_page_template( 'templates/template-name.php' )
        )
        && $item->title == 'Blog' 
    )
    {
            $classes = array_diff( $classes, ['current_page_parent'] );
    }
    return $classes;
}
add_filter( 'nav_menu_css_class', 'bb_remove_nav_classes', 10, 2 );

A lightweight, intuitive WordPress theme to enable flexible developement.

  • Lighting-fast installer
  • Intuitive SASS structure
  • Bloat-free

Build with Barebones