WordPress Custom Post Type Current Page Parent
01.13.2015
I was updating the design of this WordPress website, and I needed to highlight the portfolio link when the user is on any portfolio page. The problem was that the current_page_parent class would end up on the blog menu item. The fix to this was to add a filter to the functions.php file.
function remove_parent_classes($class) { // check for current page classes, return false if they exist. return ($class == 'current_page_item' || $class == 'current_page_parent' || $class == 'current_page_ancestor' || $class == 'current-menu-item') ? FALSE : TRUE; } function add_class_to_wp_nav_menu($classes) { switch (get_post_type()) { case 'portfolio': // we're viewing a custom post type, so remove the 'current_page_xxx and current-menu-item' from all menu items. $classes = array_filter($classes, "remove_parent_classes"); // add the current page class to a specific menu item (replace ###). if (in_array('menu-item-1342', $classes)) { $classes[] = 'current_page_parent'; } break; // add more cases if necessary and/or a default } return $classes; } add_filter('nav_menu_css_class', 'add_class_to_wp_nav_menu');
What this does is any time the nav menu gets rendered, if our current post is of type portfolio, then it will remove all parent classes from all other items, and add current_page_parent to the specific menu item that we want, in my case the portfolio link (menu-item-1342).
Sources
https://wordpress.org/support/topic/why-does-blog-become-current_page_parent-with-custom-post-type