I
I
Ivseti2021-11-27 01:09:01
WordPress
Ivseti, 2021-11-27 01:09:01

How to properly add a menu in WP 5.8 layout?

Can't add correct menu in WP. In header.php I add:

<? wp_nav_menu([
                'menu' => 'head_menu',
                'container' => false,
                'menu_class' => 'top-menu',
                'echo' => true,
                'fallback_cb' => 'wp_page_menu',
                'item_wrap' => '<ul class="header__nav">%3$s</ul>',
                'depth' => 1
                
            ]); ?>


Tried to register in two ways:
register_nav_menus( array(
  'head_menu' => 'Меню в шапке'
) );


And this:

add_theme_support( 'menus' );

I always get an error like:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\www\mebelsheldon.ru\wp-includes\class-walker-nav-menu.php on line 205


With this, the menu is displayed simply as text without forming a link:

https://skr.sh/sBDJoSzWy5G?a

How to add a menu correctly?

Update. I looked at the code on this line, the link is just being formed there. But I still don't understand what's going on.

Now it outputs like this:
<li id="menu-item-125" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-125"><a>О компании</a></li>


The a tag is there, but the link itself and href are not!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivseti, 2021-11-27
@Ivseti

Understood. Although there was an error before the function, I made the connection, as from the default theme, through the after_setup_theme action. Now everything works. The menu is displayed :)
The problem was in the hook, apparently I wrote it incorrectly ..

// Мой первый фильтр

add_filter ('nav_menu_link_attributes', 'filter_nav_menu_link_attributes', 10,3);

function filter_nav_menu_link_attributes ( $atts, $item, $args) {

  if ($args->menu === 'Main') {
    $atts['class'] = 'header__nav-item';

    if ( $item->current) {
      $atts['class'] .= 'header__nav-item-active';
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question