wordpress - 如何在 wordpress 上使用 qtranslate 翻译我的导航菜单链接?

标签 wordpress navigation qtranslate

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

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

谢谢

最佳答案

@maha,我对此进行了大量搜索并找到了解决方案 here ,但答案有点模糊……

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

找到您的主题 函数.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 );
    }
}

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

我的标题 View 代码是这样的:
<?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);
?>

您所要做的就是在 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);
?>

然后,在您的菜单中,您将使用 |语言| 在语言 URL 之后,像这样:

enter image description here

我知道这不是您跳转自动语言链接的确切用途,但这可能会解决您的问题。

关于wordpress - 如何在 wordpress 上使用 qtranslate 翻译我的导航菜单链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10102752/

相关文章:

php - 仅在 WooCommerce Admin 手动订单的单个订单中显示产品自定义字段

asp.net-mvc-4 - Durandal.js : change navigation options per area

qt - 如何在 QT 中翻译日期?

php - 使用 mqtranslate plus 在 wordpress 上翻译菜单上的自定义链接

php - 从 Wordpress 标题返回文本的简单正则表达式 - qtranslate 插件

javascript - 隐藏可滚动的标题(HTML ID)

php - 如何将目标 ="_blank"属性实现到 PHP 链接中,以便在新选项卡中打开链接?

wordpress - 如何隐藏 wp-admin AWS NginX

css - flexbox css 居中和布局不能水平工作

javascript - HTML5/JS 相当于 UINavigationController