1

I am trying to add to my menu bar some language elements. I'm using the qTranslate plugin and I would like to add the language switch to the menu bar.

My them is calling in the header.php

<?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu'));?>

And then I added to my functions.php the following code, which remains without effect :

function qtrans_generateLanguageListCode($id='') {
    global $q_config;
    if(is_404()) $url = get_option('home'); else $url = '';
    if($id=='') $id = 'qtranslate';
    $id .= '-chooser';
    foreach(qtrans_getSortedLanguages() as $language) {
        $classes = array('lang-'.$language);
        if($language == $q_config['language'])
            $classes[] = 'active';
        echo '<li class="'. implode(' ', $classes) .'"><a href="'.qtrans_convertURL($url, $language).'"';
        // set hreflang
        echo ' hreflang="'.$language.'" title="'.$q_config['language_name'][$language].'"';
        echo ' class="qtrans_flag qtrans_flag_'.$language.'"';
        echo '><span';
        echo ' style="display:none"';
        echo '>'.$q_config['language_name'][$language].'</span></a></li>';
    }
}

function new_nav_menu_items($items) {
    $translinks = qtrans_generateLanguageListCode();
    $items = $items . $translinks;
    return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );

Could anyone help me to understand what I am missing ? I have to add that I am using a child theme of Twenty Twelve, but this should be without effect.

Thanks !

  • You are echo-ing in your qTrans function, while you have to return the data. – tfrommen Jan 3 '14 at 17:22
  • Whats the tag for the switch? – Brad Dalton Jan 3 '14 at 17:24
  • @tf you are fully right, returning is much better ! Nevertheless, it is not still not returning anything... The nav bar stays empty... :-/ And I don't have reputation enough to even vote you up... normally on StackOverflow :-p – Rmatt Jan 3 '14 at 17:34
  • @BradDalton the switch is the <li> construct I am doing in the previous function – Rmatt Jan 3 '14 at 17:36
1

You have to return the data in the qtrans function:

function qtrans_generateLanguageListCode($id='') {
    global $q_config;
    if (is_404()) $url = get_option('home'); else $url = '';
    if ('' === $id) $id = 'qtranslate';
    $id .= '-chooser';
    $output = '';
    foreach (qtrans_getSortedLanguages() as $language) {
        $classes = array('lang-'.$language);
        if($language == $q_config['language'])
            $classes[] = 'active';
        $output .= '<li class="'. implode(' ', $classes) .'"><a href="'.qtrans_convertURL($url, $language).'"';
        // set hreflang
        $output .= ' hreflang="'.$language.'" title="'.$q_config['language_name'][$language].'"';
        $output .= ' class="qtrans_flag qtrans_flag_'.$language.'"';
        $output .= '><span';
        $output .= ' style="display:none"';
        $output .= '>'.$q_config['language_name'][$language].'</span></a></li>';
    }
    return $output;
}

function new_nav_menu_items($items) {
    $translinks = qtrans_generateLanguageListCode();
    $items = $items . $translinks;
    return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );
  • OK sorry @tf, this works ! I just needed to check the box to make it the Primary Menu !!! A pity I can't vote you up – Rmatt Jan 3 '14 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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