The following code added to your themes functions.php
will do what you want.
add_filter( 'walker_nav_menu_start_el', 'my_walker_nav_menu_start_el', 10, 4 );
function my_walker_nav_menu_start_el( $item_output, $item, $depth, $args ) {
if ( empty( $item->url ) || '#' === $item->url ) {
$item_output = $args->before;
$item_output .= '<span class="my-class">';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= '</span>';
$item_output .= $args->after;
}
return $item_output;
}
This should find all menu items with no URL or #
as the URL and replace the link with <span class="my-class">...</span>
.
This wouldn’t specifically apply to parent menu items, but as long as you use a custom link and don’t enter a URL (or you enter a #
) for your parent menu items then it should work for you.