0

I added a function to show a language button in nav menu, but the issue is that I have more than one menu and it duplicates the result. How can I specify what menu I want to show the language button in?

This is my code:

function new_nav_menu_items($items,$args) {

  if (function_exists('icl_get_languages') && !empty($args->theme_location)) {
    $languages = icl_get_languages();
    $current_lang = ICL_LANGUAGE_CODE;

    $nitem = "";
    if(count($languages) >= 1){
        foreach($languages as $l){
            if( $l['language_code'] != $current_lang ) {
                $nitem .= '<li class="menu-item"><a href="'.$l['url'].'"> '.$l['native_name'].'</a></li>';
            }
        }
      }
    }
  return $items . $nitem;
  }

add_filter( 'wp_nav_menu_items', 'new_nav_menu_items', 10,2 );
1
  • Try shifting add_filter( 'wp_nav_menu_items', 'new_nav_menu_items', 10,2 ); to top. That is before the function and check whether it helps mate
    – Raja Gopal
    Commented Oct 11, 2016 at 6:06

1 Answer 1

0

You are now only testing for the theme location not to be empty. This means that every non empty menu location will see the language button attached.

So, in stead of testing for emptyness you should be testing for a specific location. The name of that location depends on your theme, but suppose it's called 'primary-menu' you would have:

if (function_exists('icl_get_languages') && ($args->theme_location == 'primary-menu'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.