There is a menu in the admin panel. There is also a small class whose essence is to add a specific class to ul.

class My_Walker_Nav_Menu extends Walker_Nav_Menu { function start_lvl(&$output, $depth = 0, $args = Array()) { $indent = str_repeat("\t", $depth); $output .= "\n$indent<ul class=\"dropdown-menu\">\n"; } } 

It is necessary to convert menu items that have submenus as follows: Instead of

 <li class="menu-item-has-children"> <a>ссылка</a> </li> 

withdraw

 <li class="menu-item-has-children"> <a>ссылка <span>что-то тут</span></a> </li> 

tell me how to do this?

  • and where is the code in which you are adding the tag <li> ? - Evgeny Nikolaev
  • in the standard Walker - denism300

1 answer 1

Solution found: It is necessary to supplement the above-presented class My_Walker_Nav_Menu as follows:

 class My_Walker_Nav_Menu extends Walker_Nav_Menu { function start_lvl(&$output, $depth = 0, $args = Array()) { $indent = str_repeat("\t", $depth); $output .= "\n$indent<ul class=\"dropdown-menu\">\n"; } function start_el(&$output, $item, $depth, $args) { // назначаем классы li-элементу и выводим его $class_names = join( ' ', $item->classes ); $class_names = ' class="' .esc_attr( $class_names ). '"'; $output.= '<li id="menu-item-' . $item->ID . '"' .$class_names. '>'; // назначаем атрибуты a-элементу $attributes.= !empty( $item->url ) ? ' href="' .esc_attr($item->url). '"' : ''; $item_output = $args->before; // проверяем, есть ли класс menu-item-has-children и, если есть, модифицируем ссылку if (strpos($class_names, 'menu-item-has-children')) $item_output.= '<a'. $attributes .'>'.$item->title.'<span></span></a>'; else $item_output.= '<a'. $attributes .'>'.$item->title.'</a>'; // заканчиваем вывод элемента $item_output.= $args->after; $output.= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); } }