2

I have a theme that uses Advanced Custom Fields to allow the user to select different header options. One of the header options (where the logo sits in the middle of the page between two menus) requires the registration of three new menus, one left menu, a right menu, and a mobile menu.

When you select the header option, it loads a separate nav-left-logo-mid-nav-right.php file that creates the HTML for the specific header style.

I have the following code to create the menu:

add_action( 'after_setup_theme', 'menu_left_logo_mid_menu_right' );
function menu_left_logo_mid_menu_right() { 
    register_nav_menus( 
        array( 'left_menu'   => 'Left Menu', 
               'right_menu'  => 'Right Menu', 
               'mobile_menu' => 'Mobile Menu' )
     ); 
}

However, I can't get it to work outside of functions.php. I tried adding the add_action to the nav-left-logo-mid-nav-right.php file but the menus didn't register. No matter what I do I can't get the register_nav_menus() code to run outside functions.php.

I only want these new menu locations registered when that header option is selected. However, I can't access the Advanced Custom Fields in functions.php, so I'm not really sure to get this to work.

Does anyone know how to get register_nav_menus() to work outside of functions.php?

1 Answer 1

1

You can access ACF values in functions.php and you'll have to.

Registering menus in template file won't work, because templates are loaded after after_setup_theme is run. So if you add any action to that hook in template file, that action won't run - they're registered to late...

2
  • Wow, this worked like a charm. All I did was add an if statement around the add_action statement. I don't know why this didn't work the first time I tried it. Thanks.
    – qotsa42
    Jul 31, 2018 at 20:49
  • @qotsa42 No problem :) Jul 31, 2018 at 20:50

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.