我目前正在使用Transposh(但我不介意另外使用其他东西)并且我有一个菜单项指向PDF文件(用户下载)。
该PDF文件以网站支持的4种语言存在,但我找不到为每个语言自定义菜单项的href的方法。
我怎样才能做到这一点?
答案 0 :(得分:1)
如果我正确理解您的问题,您(或您的用户)可以将pdf链接添加到菜单中,例如:
http://example.com/prices_2014_%pdflang%.pdf
其中%pdflang%
是当前语言。
然后在当前主题目录中的functions.php
文件中使用以下过滤器,或将其放在插件中:
/**
* Replace %pdflang% in your nav menus, with the current language string.
*
* @uses transposh_get_current_language()
* @see http://stackoverflow.com/a/25721273/2078474
*/
! is_admin() && add_filter( 'wp_nav_menu_objects',
function( $items, $args ) {
if( function_exists( 'transposh_get_current_language' ) )
{
// Get current language:
$pdflang = sanitize_key( transposh_get_current_language() );
foreach ( $items as $item )
{
if( false !== strpos( $item->url, '%pdflang%' ) )
{
// Replace %pdflang% with current language key:
$item->url = str_replace(
'%pdflang%',
$pdflang,
$item->url
);
}
}
}
return $items;
}
, 11, 2 );
在菜单项链接中使用当前语言键自动替换%pdflang%
字符串。
然后上面的链接将转换为:
http://example.com/prices_2014_en.pdf // English as current language
http://example.com/prices_2014_fr.pdf // France as current language
http://example.com/prices_2014_da.pdf // Danish as current language
http://example.com/prices_2014_is.pdf // Icelandic as current language
然后,您可以轻松地将其修改为仅定位特定菜单。
答案 1 :(得分:0)
由于您使用的是Transposh,因此可以通过JavaScript实现此目的。
以下是示例代码:
<script type="text/javascript">
//This gets the language of the current page the user is on
var language = document.querySelector('.tr_active').children[0].getAttribute("title");
//#pdfLink is the anchor tag with the ID pdfLink in your page which you intend to customize
var pdfLink = document.querySelector('#pdfLink');
if (language === "English")
{
pdfLink.href = "http://path/to/englishFile.pdf";
}
else if (language === "Français")
{
pdfLink.href = "http://path/to/frenchFile.pdf";
}
else if ...(other language conditions)...
</script>
答案 2 :(得分:0)
你知道,你可以使用一个名为“WPML”的翻译插件,它可以让一切变得更加容易。 根据用户正在查看的语言,在条件中显示内容也很容易。
像这样:
<?php if(ICL_LANGUAGE_CODE == 'en') { ?>
Display the english PDF here
<?php } elseif (ICL_LANGUAGE_CODE == 'it') { ?>
Display the italian PDF here
<?php } else {} ?>
ICL_LANGUEAGE_CODE
位基本上获取当前正在查看的语言。
你抓住了我的漂移?
链接到他们的网站:http://wpml.org/
文档链接:http://wpml.org/documentation/
感谢。