独特的PDF取决于我的WordPress站点上的语言
我目前正在使用Transposh(但我不介意使用别的东西另外),我有一个菜单项指向PDF文件(用户下载)。独特的PDF取决于我的WordPress站点上的语言
该PDF文件存在网站支持的4种语言,但我没有找到一种方法来自定义每个lang的菜单项的href。
我该如何做到这一点?
由于您正在使用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>
我很欣赏这种努力,但它会杀死“用户友好CMS”的概念。本网站旨在由对编码一无所知的人定制。 – 2014-09-08 08:18:16
对于非编码答案,您可以在[WebApps StackExchange站点](http://webapps.stackexchange.com/)或[Transposh的WordPress支持论坛](http://wordpress.org)上获得更好的运气。 /支持/插件/通过Transposh翻译过滤换WordPress的)。 – 2014-09-08 08:25:02
如果我正确理解您的问题,您(或您的用户)可以添加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
此后可以轻松地修改此目标只有一个特定的菜单。
您知道,您可以使用名为“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/
感谢。
您可以将href创建为一个php文件,然后重定向到正确的pdf文件。 – d3L 2014-09-08 07:16:58
编写代码将是我的最后手段。我很确定我不是第一个需要该功能的WordPress用户。 – 2014-09-08 07:31:13
pdf文件的名称是什么?你的概率是你不能?链接4个不同版本的pdf取决于语言到一个'下载'链接? – 2014-09-08 07:32:34