I use this menu code in functions.php
class main_menu_walker extends Walker_Nav_Menu { function start_el(&$output, $item, $depth, $args) { global $wp_query; $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; $class_names = $value = ''; $classes = empty( $item->classes ) ? array() : (array) $item->classes; $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ); $class_names = ' class="'. esc_attr( $class_names ) . ' nav-item"'; $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>'; $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : ''; $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : ''; $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : ''; $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : ''; $item_output = $args->before; if ( $args->walker->has_children && 0 === $depth ) { $item_output .= '<span class="nav-link">'; } else { $item_output .= '<a class="nav-link" ' . $attributes . '>'; } $item_output .= $args->link_before .apply_filters( 'the_title', $item->title, $item->ID ); $item_output .= ( $args->walker->has_children && 0 === $depth ) ? ' </span>' : '</a>'; $item_output .= $args->after; $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); } } I get the following structure :
<ul class="nav navbar-nav nav__top-menu"> <li class="nav-item"><a class="nav-link" href="#">Home</a></li> <li class="nav-item"><span class="nav-link">About </span> <ul class="sub-menu"> <li class="nav-item"><a class="nav-link" href="#">text 1</a></li> <li class="nav-item"><a class="nav-link" href="#">text 2</a></li> </ul> </li> </ul> If the parent link has a submenu then it becomes a span .
How to make the transition to the first submenu page when clicking on the parent link?
UPD
Found such a plugin . But he finds the child page that was published first function has_published_children , and in the menu the position of the items may vary. Those. You need a search function not by the date the page is published, but by its position in the menu.
spanyou can inserta- and on the other, there may be a js handler on this menu, and it is not clear how it will behave. - Ivan Bolnikh