Having trouble with Walker_Nav_Menu (it’s duplicating my menu) …
-
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.
- The topic ‘Having trouble with Walker_Nav_Menu (it’s duplicating my menu) …’ is closed to new replies.