I need help with menu translate. I’m created a two different menus for two pages “Landing” and “Blog”. And my code worked fine until I started translating with Polylang.
Here is the menu code:
<?php
if ( is_page( 'landing' ) ) {
wp_nav_menu( array(
'menu' => 'landing',
'menu_class' => 'nav navbar-nav darka',
'container' => 'false',
) );
} else {
wp_nav_menu( array(
'menu' => 'blog',
'menu_class' => 'nav navbar-nav darka',
'container' => 'false',
) );
}
?>
This code created menu, if help:
add_theme_support( 'menus' );
function register_theme_menus() {
register_nav_menus(
array(
'landing-menu' => __('Landing Menu'),
'blog-menu' => __('Blog Menu')
)
);
}
add_action('init', 'register_theme_menus');
UPD:
I solved this problem!
functions.php
<?php
add_theme_support( 'menus' );
// create menu
function register_theme_menus() {
register_nav_menus(
array(
'primary' => __('Primary Menu', 'nameyourtheme'),
'second' => __('Second Menu', 'nameyourtheme')
)
);
}
add_action('init', 'register_theme_menus');
?>
header.php
<?php
if (is_front_page()) {
wp_nav_menu( array(
'theme_location' => 'primary',
'menu_class' => 'nav navbar-nav darka',
'container' => 'false',
) );
}
else {
wp_nav_menu( array(
'theme_location' => 'second',
'menu_class' => 'nav navbar-nav darka',
'container' => 'false',
) );
}
?>
I had the same issue with translating more than one menus but my case was a bit different. In my case the two menus were on the homepage located in the header.
functions.php
This was my original theme menus. Then I removed the underscore like so:
and it worked. Ofcourse the change in the menu name should be applied in the
header.php
My conclusion is that Polylang recognizes only a single word menu name.
Not sure if this can help, but rather than registering menus, I usually name my menus like this:
Then the website can simply load the right one with a one liner using wordpress get_locale().
I find this easier than registering menus.