Remove current_page_parent from posts page link in WordPress nav menu

I have set a page to be the posts page. So when I visit single posts the link for posts page in nav menu has a class ‘current_page_parent’.

But when I visit single posts for a custom post type, then also it is adding ‘current_page_parent’ to the link for posts page.

How can I restrict it and not add the class when I visit single custom post types page.

1 Answer
1

I assuming you’re using wp_page_menu() or wp_list_pages(). If so, then what you need to do is hook into the page_css_class filter, e.g.,

add_filter ('page_css_class', 'my_func', 10, 5) ;

function
my_func ($classes, $page, $depth, $args, $current_page_id)
{
    if (/* test condition */) {
        $classes = array_diff ($classes, array ('current_page_parent')) ;
        }

    return ($classses) ;
}

where /* test condition */ is where you’d put your logic to decide when that class should be included.

Leave a Comment