Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1365096

    Hi there,

    I’ve seen a lot of those questions here, but none of them worked for me or weren’t suitable for my project.

    We got 3 menus which can be selected to be displayed on pages. To do that, I’ve created a custom field with Advanced Custom Fields and written the separate IDs of the 3 menus as options. So far, so good.
    But when it comes to changing the menu, it just won’t work. I’ve edited the “helper-main-menu.php” and added the header ID to the arguments before calling wp_nav_menu:

    if(get_field('header_selection')) {
    			$headerID = get_field('header_selection');
    			$args = array(
    					'menu' 				=> $headerID, 
    					'theme_location'	=> $avia_theme_location,
    					'menu_id' 			=> $avia_menu_class,
    					'menu_class'		=> 'menu av-main-nav',
    					'container_class'	=> $avia_menu_class.' av-main-nav-wrap'.$icon_beside,
    					'items_wrap'        => '<ul role="menu" class="%2$s" id="%1$s">%3$s</ul>',
    					'fallback_cb' 		=> 'avia_fallback_menu',
    					'echo' 				=>	false,
    					'walker' 			=> new avia_responsive_mega_menu()
    			);
    } else { ..(args without menu-id).. }

    But it just wont display the right menu. If I remove “theme_location”, it will work, but the styling is way off and it isn’t really working perfectly.

    As you can see, I’m not afraid to code, so please don’t suggest other plugins. I really need that to be done manually with ACF.

    Thanks in advance!

    • This topic was modified 2 years ago by emilconsor.
    #1365132

    Hey,

    Thanks for contacting us!

    Here is how I would do:
    – Choose Select type and add 4 choices including a default one which would be your global menu – https://imgur.com/a/J6qcq9Q
    – Add following code to bottom of Functions.php file of child theme

    
    add_filter("wp_nav_menu_args", "my_custom_wp_nav_menu_args");
    function my_custom_wp_nav_menu_args($args = "")
    {
        if ($args["theme_location"] === "avia") {
            if (get_field("menu_selector") == "menu_1") {
                $args["menu"] = "Menu1";
            } elseif (get_field("menu_selector") == "menu_2") {
                $args["menu"] = "Menu2";
            } elseif (get_field("menu_selector") == "menu_3") {
                $args["menu"] = "Menu3";
            } else {
                $args["menu"] = "Main Menu";
            }
        }
        return $args;
    }
    

    Menu1, Menu2 and Menu3 are the menu names created in Appearance > Menus

    Best regards,
    Yigit

    #1365145

    Hi Yigit,

    thank you for your snippet, this is cleaner than overwriting the helper file.
    I’ve noticed that this works if mega menu isn’t active. Do you know if I can get it to work the same way with mega menu as active plugin?

    #1365149

    Hi,

    Are you using Enfold’s mega menu or a separate plugin? I just chose to display one of the menu items as a mega menu in one of the menus I created but it works fine on my end.

    Best regards,
    Yigit

    #1365150

    OK I think I got it..
    I’ve added a new location with Mega Menu which created a theme_location, and based of my selection I change the theme_location and the menu and now it works.

    Thanks for your help!

    #1365151

    Hey,

    You are welcome!

    Let us know if you have any other questions and enjoy the rest of your day :)

    Regards,
    Yigit

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Display different header based on selected field (ACF)’ is closed to new replies.