如何在wordpress上使用qtranslate翻译导航菜单的LINKS?

mah*_*aha 4 navigation wordpress qtranslate

我有一个双语(英语/阿拉伯语)wordpress网站.我能够成功翻译导航菜单项.但是,阿拉伯语网站上的菜单链接,链接到英语的默认语言.

我怎么能告诉wordpress我需要在阿拉伯网站上更改菜单链接(我需要阿拉伯网站上的链接包含/ ar,例如:www.talalonline.com/ar而不是www.talalonline.com)

谢谢

Edu*_*sso 10

@maha,我搜索了很多关于这个并在这里找到了解决方案,但答案有点模糊......

由于您不想弄乱WP核心文件,所有更改都在主题中.您的主题位于wp-content/themes/your-theme-name /

找到你的主题的function.php,并在php结束标记(?>)之前在文件末尾添加上面的代码:

class CustomLinkModifierWalker extends Walker_Nav_Menu {
    function __( $text ) {
        if ( preg_match_all('~(.*?)\|(\w{2,})\|~', $text, $matches) ) {
            $text = '';
            foreach ($matches[1] as $i => $match) {
                $text .= "[:{$matches[2][$i]}]$match";
            }
            $text = __( $text );
        }
        return $text;
    }
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        global $wp_query;
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

        $class_names = $value = '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';

        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';

        $output .= $indent . '<li' . $id . $value . $class_names .'>';

        $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
        $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
        $attributes .= ! empty( $item->url )    ? ' href="' . esc_attr( $this->__( $item->url )  ) .'"' : '';

        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您必须在主题中找到菜单视图的位置.我正在使用的主题是在header.php中实现的.也许你的使用另一个文件名,比如header-fancy-theme.php.

我的标题视图代码是这样的:

<?php
$nav_sec_menu_params = array(
    'depth' => 0,
    'theme_location' => 'sec-menu',
    'container_class' => 'menu-topmenu-container',
    'menu_class' => 'menus menu-topmenu',
    'fallback_cb' => 'block_sec_menu'
);
wp_nav_menu($nav_sec_menu_params);
?>
Run Code Online (Sandbox Code Playgroud)

您所要做的就是在param数组中添加Walker实现:

<?php
$nav_sec_menu_params = array(
    'walker' => new CustomLinkModifierWalker(),
    'depth' => 0,
    'theme_location' => 'sec-menu',
    'container_class' => 'menu-topmenu-container',
    'menu_class' => 'menus menu-topmenu',
    'fallback_cb' => 'block_sec_menu'
);
wp_nav_menu($nav_sec_menu_params);
?>
Run Code Online (Sandbox Code Playgroud)

然后,在您的菜单中,您将使用| lang | 在语言URL之后,如下所示:

在此输入图像描述

我知道这不是您使用自动语言链接的确切用途,但这可以解决您的问题.