Show different different menu in theme_location depending on x

Ok I managed to fix this. Posting the answer here for anyone interested.

I didn’t quite understand that register_nav_menus only registeres menu locations.
This is the code I used which does conditionally use different named menus in the same location. For this to work, you have to use the exact name specified when creating the menus. Or, as I did, create them programmatically to make sure they always exists and are named correctly.

<?php
    if ( preg_match ('#^/company/#', $_SERVER['REQUEST_URI']) ) {
        wp_nav_menu( array( 'menu' => 'company-primary', 'theme_location' => 'primary', 'fallback_cb'=> false  ) );
    }
    else {
        wp_nav_menu( array( 'menu' => 'private-primary', 'theme_location' => 'primary', 'fallback_cb'=> false  ) );
    }
?>

I used this code to set up some default menus:

// Set up default menus
$private_menu_exists = wp_get_nav_menu_object( 'private-primary' );
if( !$private_menu_exists){
    $menu_id = wp_create_nav_menu( 'private-primary' );
}
$company_menu_exists = wp_get_nav_menu_object( 'company-primary' );
if( !$company_menu_exists){
    $menu_id = wp_create_nav_menu( 'company-primary' );
}

Leave a Comment