How can I programmatically change the menu in Wordpress?
I have created one menu in Norwegian menu_no
and one in English menu_en
.
I see that my theme only supports one menu, but I am not planning on using more than one menu at a time. So, when the user selects English, how can I change the active menu?
I haven't found anything on Google and I can't seem to find the function I want in nav-menu.php
.
UPDATE
I found a pretty simple solution for my problem. I just needed to think differently. In mine, functions.php
I added this code:
add_action('init', 'register_menus');
function register_menus(){
register_nav_menus( array(
'menu_no' => 'Norwegian menu',
'menu_en' => 'English menu',
) );
}
and in my header.php
file i am using this code:
global $lang;
$args = array(
'theme_location' => 'menu_'.$lang,
'container' => false
);
<?php wp_nav_menu($args); ?>
Voila. I'll post it as an answer later - if not someone else comes up with a better idea.
source to share
Here's how you can do it.
Will pass this code into functions.php file
// Register Navigation Menus
function custom_navigation_menus() {
$locations = array(
'first_menu' => __( 'Menu for English language', 'text_domain' ),
'second_menu' => __( 'Menu for Other Language', 'text_domain' ),
);
register_nav_menus( $locations );
}
// Hook into the 'init' action
add_action( 'init', 'custom_navigation_menus' );
You can customize the above code. Now use the wp_nav_menu function to display anywhere you want,
<?php
wp_nav_menu( array(
'menu' => 'primary',
'theme_location' => 'first_menu',
);
?>
<?php
wp_nav_menu( array(
'menu' => 'secondary',
'theme_location' => 'second_menu',
);
?>
You can display each menu in a different location. You are now in Option under the "Primary" and "Secondary" menus
source to share