You can filter wp_nav_menu_objects
and add the classes in one rush. You don’t even need a custom walker for that.
add_filter( 'wp_nav_menu_objects', 'add_has_children_to_nav_items' );
function add_has_children_to_nav_items( $items )
{
$parents = wp_list_pluck( $items, 'menu_item_parent');
foreach ( $items as $item )
in_array( $item->ID, $parents ) && $item->classes[] = 'has-children';
return $items;
}
The class has-children
will now be assigned to the matching li
elements automatically.
In a custom walker you will find the class in $item->classes
in your function start_el()
. The following lines from the default walker will do the trick:
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';