4

I wrote this simple code to prove what I'm saying:

wp_nav_menu( [
    'menu' => 'Primary Navigation',
    'menu_class' => 'this-is-the-menu-class',
    'menu_id' => 'this-is-the-menu-id',
    'container' => 'div',
    'container_class' => 'this-is-the-container-class',
    'container_id' => 'this-is-the-container-ID',
] );

This is the output:

<div id="this-is-the-menu-id" class="this-is-the-menu-class">
    <ul>
        <li class="page_item page-item-2">
            <a href="http://localhost/test_site/index.php/pagina-di-esempio/">Example Page.</a>
        </li>
    </ul>
</div>

As you can see the ul element doesn't receive any class nor ID.

They, instead, are applied to the container and the container classes and ID seem to be simply missed (or getting the default values).

In the documentation is told that

  • 'menu_class' (string) CSS class to use for the ul element which forms the menu. Default 'menu'.
  • 'menu_id' (string) The ID that is applied to the ul element which forms the menu. Default is the menu slug, incremented.
  • 'container' (string) Whether to wrap the ul, and what to wrap it with. Default 'div'.
  • 'container_class' (string) Class that is applied to the container. Default 'menu-{menu slug}-container'.
  • 'container_id' (string) The ID that is applied to the container.

But as you can see the result is not the one expected.

In this discussion the user set wrong the container param, and so the replies point out this error, but the error still exists and I'm doing all following the rules (at least I think!).

Am I doing something wrong or effectively the function is bugged?

NOTEs:

  • I'm using Genesis Framework (just in case it may be useful to understand the context)
  • Wordpress version is 4.9.4

3 Answers 3

5

I believe this is caused probably by your theme or another plugin, filtering on wp_nav_menu_args and changing the items_wrap value. I tested your provided code and the output was correct, using blank WordPress 4.9.4 installation.

You could try passing items_wrap with default value:

wp_nav_menu( [
    'menu' => 'Primary Navigation',
    'menu_class' => 'this-is-the-menu-class',
    'menu_id' => 'this-is-the-menu-id',
    'container' => 'div',
    'container_class' => 'this-is-the-container-class',
    'container_id' => 'this-is-the-container-ID',
    'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>'
] );

But if something is filtering that out, it wouldn't make a difference.

I recommend adding this to your child theme's functions.php file, to remove all filters and see if that fixes it (to prove it is a filter causing your problem):

remove_all_filters( 'wp_nav_menu_args' );

You can also add this to your child theme's functions.php, to dump out on screen, the $args array, where you can check the value of items_wrap:

add_filter( 'pre_wp_nav_menu', 'smyles_dump_nav_menu_args', 9999, 2 );

function smyles_dump_nav_menu_args( $null, $args ){
    ob_start();

    echo '<pre>';
    var_dump($args);
    echo '</pre>';

    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}

Returning a true value (in our case, it's HTML) to this filter will cause it to echo on page, when echo is passed true in the wp_nav_menu

Make sure to add echo and set true in wp_nav_menu call:

wp_nav_menu( [
    'menu' => 'Primary Navigation',
    'menu_class' => 'this-is-the-menu-class',
    'menu_id' => 'this-is-the-menu-id',
    'container' => 'div',
    'container_class' => 'this-is-the-container-class',
    'container_id' => 'this-is-the-container-ID',
    'echo' => true
] );
2
  • 1
    Ok, I've tried your suggestions. Removing all filters through remove_all_filters( 'wp_nav_menu_args' ); doesn't solve the problem. Printing out the arguments, show them correctly (with and without the removal of all filters): so it seems that the problem exists, but isn't caused by any filter... On the installation I'm using there is only Genesis Framework installed and the child theme I'm working on. But it isn't the cause as I haven't added nothing related to menu. The problem is on Genesis side: I've to discover where it is... Suggestions?
    – Aerendir
    Mar 9, 2018 at 10:51
  • did you find a solution to this? Jun 23, 2021 at 4:22
0

Just ran into this exact problem : the menu_class going on the container and the ul having no class at all.

Turns out this was due to me forgetting to check a menu location when creating the menu in my admin panel.

Instead of giving you an error, wordpress uses a default menu location and for some weird behind-the-scene reason it causes this.

2
-1

I had this same problem and came to the conclusion that this was happening because I had not created the menu in the wordpress admin panel.

What you have to do is create a menu in the admin panel and use that menu to locate the menu you created in the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.