How to conditionally register nav menus with advanced custom fields?

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?

Read more here:: How to conditionally register nav menus with advanced custom fields?

Leave a Reply

Your email address will not be published. Required fields are marked *