Can’t include Language switch in Nav Menu

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

Read More
<?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 !

Related posts

1 comment

  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' );
    

Comments are closed.