• Resolved Nox

    (@profnox)


    Hi,

    I’m using ACF to add some fields on Menu Items, which I want to render inside the menu item itself, in order to create a kind of “mega-menu”.

    So I read about “Walker_Nav_Menu” class and created a custom walker. I was trying to get my field inside the start_el function. Until here, it works : my field is recognized if the item has one.

    I added it in the output like this :

            if (get_field('column_description', $item->ID));
               $item_output .= '<div class="description">'.get_field('column_description', $item->ID).'</div>';

    And it well displays the content inside the item.

    … But the problem is that the walker isn’t transforming the existing menu at all. It creates another primary menu, directly after the <body> tag. So I have two primary menu now, the new one above the page… I searched a lot on some tutorials and so I deduce that this comportement isn’t normal at all.

    Here’s in details what I made on my theme :

    1. I registered two menus, respectively placed in the header and the footer (But in this case I’m working on the header menu only) :

        register_nav_menus( array(
            'header' => 'Header menu',
            'footer' => 'Footer menu'
        ) ); 

    2. Called my menu in header.php file :

                    <nav id="header-navigation" class="header-navigation" role="navigation">
                        <?php wp_nav_menu( array( 'theme_location' => 'header', 'menu_class' => 'nav-menu', 'fallback_cb' => false ) ); ?>
                    </nav>

    3. Created my ACF fields, and add some items to my menu (no need of details here)…

    4. Created the walker (it’s a bit long because of initial comments, so I will share a Pastebin) : https://pastebin.com/48KEAzbE
    Please jump to the comment “// Here is my change”

    5. Included this file to “functions.php” and add thoses lines :

    wp_nav_menu(
        array(
            'theme_location'    =>  'header',
            'walker'            =>  new My_Nav_Menu_Walker()
        )
    );

    Has anybody an idea on the issue ?

    Thank you a lot in advance,

    N.C.

    • This topic was modified 6 years, 7 months ago by Nox.
    • This topic was modified 6 years, 7 months ago by Nox.
    • This topic was modified 6 years, 7 months ago by Nox.
    • This topic was modified 6 years, 7 months ago by Nox.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s because you are calling wp_nav_menu() twice! First when the theme loads, the call in functions.php outputs a menu. This creates invalid HTML, but browsers tend to render it anyway. Then when header.php is loaded wp_nav_menu() is called again, outputting the other menu.

    Remove the call in functions.php and use it to replace the call in header.php.

    Thread Starter Nox

    (@profnox)

    Oh dear… How can I be so silly. I did not even notice that it was the same functions. Thank you & sorry.

    Moderator bcworkz

    (@bcworkz)

    No worries, silly lapses happen to all of us. Getting fresh eyes on it can save a lot of frustration.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Having trouble with Walker_Nav_Menu (it’s duplicating my menu) …’ is closed to new replies.