WordPress, how translate two different menus correctly with Polylang?

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.

Look for images

Read More

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',
  ) );   
}
?>

Related posts

Leave a Reply

2 comments

  1. 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

    'top_menu' => __('Top Menu', 'nameyourtheme')
    

    This was my original theme menus. Then I removed the underscore like so:

    'topmenu' => __('Top Menu', 'nameyourtheme')
    

    and it worked. Ofcourse the change in the menu name should be applied in the

    header.php

    'theme_location' => 'topmenu',
    

    My conclusion is that Polylang recognizes only a single word menu name.

  2. Not sure if this can help, but rather than registering menus, I usually name my menus like this:

    Menu fr_CA
    Menu en_US
    

    Then the website can simply load the right one with a one liner using wordpress get_locale().

    <? wp_nav_menu(array('menu' => 'Menu '.get_locale())); ?>
    

    I find this easier than registering menus.