0%

Translatepress Custom Language Switcher

自定义 Wordpress Translatepress 语言切换菜单。

主题模板用的:
https://themeforest.net/item/optime-logistics-transportation-wordpress-theme/24380838
由于官方只兼容了WPML和自己的语言切换器,未兼容 Translatepress ,所以需要自己加上。

编辑主题文件,找到function.php 加入:
ref: https://translatepress.com/docs/developers/custom-language-switcher/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function trpc_custom_language_switcher()
{
if (apply_filters('trp_allow_tp_to_run', true)) {
$html = '<div class="language-dropdow">';
$languages = trp_custom_language_switcher();
$html .= "<ul data-no-translation>";
$html .= '<li class="lang-item">';
$html .= '<a class="lang-sel">' . get_locale() . "</a>";
$html .= '<ul class="lang-submenu">';
foreach ($languages as $name => $item) {
$html .= '<li class="lang-item">';
$html .= "<a href='{$item['current_page_url']}'>";
$html .= "<span>{$item['language_code']}</span></a></li>";
}
$html .= "</ul>";
$html .= "</li>";
$html .= "</ul>";
$html .= "</div>";
return $html;
}
}

打开 template-parts/header-layout.php 在判断语言菜单处加入:

1
2
3
4
...
<?php } elseif ( class_exists('TRP_Translate_Press') ) { ?>
<?php echo trpc_custom_language_switcher(); ?>
...

欢迎关注我的其它发布渠道