I slightly fiddled with the Walker_Nav_Menu
class in wordpress, the goal was to replace the standard menu classes with my own.
Here is the code:
function start_el (& amp; $ output, $ item, $ depth, $ args) {
// here I add my values to the array with classes.
// assign classes to the li element and display it
$ item- & gt; classes ['my_item_class'] = 'page-header__nav_item'; // class of li element
$ item- & gt; classes ['my_link_class'] = 'page-header__nav_link'; // class a of the element
if ($ item- & gt; current) {
$ item- & gt; classes ['my_current_class'] = 'page-header__nav_link - active'; // class a of the element of the current menu item
}
$ class_names = 'class = "' .esc_attr ($ item- & gt; classes ['my_item_class']. ''. $ item- & gt; classes ['parent_class']). '"';
$ output. = '& lt; li'. $ class_names. '& gt;';
// assign attributes to a-element
$ attributes. =! empty ($ item- & gt; url)? 'href = "' .esc_attr ($ item- & gt; url). '" class = "'. $ item- & gt; classes ['my_link_class']. ''. $ item- & gt; classes ['my_current_class']. '"': '';
$ item_output = $ args- & gt; before;
array with classes $ item- & gt; classes
, after these manipulations it looks like:
Array
(
[0] = & gt;
[1] = & gt; menu-item
[2] = & gt; menu-item-type-custom
[3] = & gt; menu-item-object-custom
[4] = & gt; current-menu-item
[5] = & gt; current_page_item
[6] = & gt; menu-item-home
[my_item_class] = & gt; page-header__nav_item
[my_link_class] = & gt; page-header__nav_link
[my_current_class] = & gt; 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, and, accordingly, for all the elements of the classes
array added by me.
What’s the problem?
Answer 1, authority 100%
This should fix the problem. As I understand it, the problem is related to the PHP version.
if (! isset ($ item- & gt; classes ['my_item_class'])) // fix
{
$ item- & gt; classes ['my_item_class'] = 'page-header__nav_item'; // class li
}