导航菜单中不能包含语言切换

时间:2014-01-03 作者:Rmatt

我正在尝试向菜单栏添加一些语言元素。我正在使用qTranslate插件,我想将语言开关添加到菜单栏。

我的他们在标题中呼叫。php

<?php wp_nav_menu( array( \'theme_location\' => \'primary\', \'menu_class\' => \'nav-menu\'));?>
然后我添加到我的函数中。php以下代码仍然无效:

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\' );
谁能帮我理解我错过了什么?我必须补充一点,我使用的儿童主题是“二十一二”,但这应该是无效的。

谢谢

1 个回复
最合适的回答,由SO网友:tfrommen 整理而成

你必须return qtrans函数中的数据:

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

结束

相关推荐

Wordpress Navigation

我的菜单html代码如下:<div class=\"menu\"> <ul> <li><a href=\"index.php\" >Home</a></li> <li><a href=\"aboutus.php\" >About Us</a></li> <li><a href=\