I’m using the following code to achieve this. Doesn’t need any walkers or anything else, just insert this into your functions.php and the items with sub-menu will get “menu-parent-item” added as class.
add_filter( 'wp_nav_menu_objects', 'add_menu_parent_class' );
function add_menu_parent_class( $items ) {
$parents = array();
foreach ( $items as $item ) {
//Check if the item is a parent item
if ( $item->menu_item_parent && $item->menu_item_parent > 0 ) {
$parents[] = $item->menu_item_parent;
}
}
foreach ( $items as $item ) {
if ( in_array( $item->ID, $parents ) ) {
//Add "menu-parent-item" class to parents
$item->classes[] = 'menu-parent-item';
}
}
return $items;
}
I’ve been using this for a long time. Unfortunately I can’t remember where I found it, so can’t give credits to the one who came up with it.