The Wordpress Walker_Nav_Menu class was a bit crowded, the goal was to replace the standard menu classes with its own.
Here is the code:

 function start_el(&$output, $item, $depth, $args) { // здесь дописываю к массиву с классами свои значения. // назначаем классы li-элементу и выводим его $item->classes['my_item_class'] = 'page-header__nav_item'; // класс li элемента $item->classes['my_link_class'] = 'page-header__nav_link'; // класс a элемента if($item->current) { $item->classes['my_current_class'] = 'page-header__nav_link--active'; // класс a элемента текущего пункта меню } $class_names = ' class="' .esc_attr( $item->classes['my_item_class'] .' '. $item->classes['parent_class'] ). '"'; $output.= '<li ' .$class_names. '>'; // назначаем атрибуты a-элементу $attributes.= !empty( $item->url ) ? ' href="' .esc_attr($item->url). '" class="' . $item->classes['my_link_class'] . ' ' . $item->classes['my_current_class'] . '" ' : ''; $item_output = $args->before; 

array with classes $item->classes , after these manipulations it looks like:

 Array ( [0] => [1] => menu-item [2] => menu-item-type-custom [3] => menu-item-object-custom [4] => current-menu-item [5] => current_page_item [6] => menu-item-home [my_item_class] => page-header__nav_item [my_link_class] => page-header__nav_link [my_current_class] => page-header__nav_link--active ) 

An error occurs:

Warning: Illegal string offset 'my_item_class' in /var/www/vhosts/u0205156.plsk.regruhosting.ru/blog.itconcentrate.pw/wp-content/themes/gragon/functions.php on line 221

Well, respectively, for all the elements of the classes array added by me.
What is the problem?

  • this means that the key does not exist 'my_item_class' - L. Vadim
  • @ L. Vadim, how not to exist? he is there !!! print_r($item->classes) it shows !!! Classes are added to the menu items, only warning climbs. - pepel_xD

1 answer 1

This should solve the problem. As I understand it, the problem is with the PHP version.

  if(!isset($item->classes['my_item_class'])) // фикс { $item->classes['my_item_class'] = 'page-header__nav_item'; // класс li } 
  • Yes, thanks, figured it out. It is necessary to check whether the necessary key exists in the array, then form a string with classes of elements. Since not all menu items have classes such as parent and current , it is necessary to check if there are appropriate keys in the array. - pepel_xD