My solution to this problem when extending the Walker_Nav_Menu was to skip check if current_page_parent
when C.P.T. I identified C.P.Ts following this stackexchange post.
This does not entirely solve the problem as I am using current_page_item
for C.P.T parent menu items, but it does achieve the desired outcome. Here is my class
.
class My_Menu extends Walker_Nav_Menu {
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$post_type = get_post_type();
$class_names = '';
// Check if post_type - $item->attr_title returns the WP Menu custom link Title Attribute
$class_names .= ($item->attr_title != '' && $item->attr_title == $post_type || in_array("current_page_item", $item->classes)) ? 'current_page_item' : '';
// Prevent Blog menu item from getting the current_page_parent class when Custom Post Type
if(!is_post_type_archive() && !is_singular(array( 'custom-one', 'custom-two'))){
$class_names .= in_array("current_page_parent", $item->classes) ? ' current_page_parent' : '';
}
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$output .= '<li' . $id . $class_names .'>';
$atts = array();
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</a>';
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
$output .= "</li>\n";
}
} // Walker_Nav_Menu