取决于我的wordpress网站上的语言的不同PDF

取决于我的wordpress网站上的语言的不同PDF,wordpress,multilingual,Wordpress,Multilingual,我目前正在使用Transposh(但我不介意另外使用其他东西),我有一个指向PDF文件的菜单项(用户下载) 该PDF文件存在于该网站支持的4种语言中,但我找不到为每个语言自定义菜单项href的方法 如何实现这一点?因为您使用的是转置,所以可以通过JavaScript实现这一点 下面是一个示例代码: <script type="text/javascript"> //This gets the language of the current page the user is o

我目前正在使用Transposh(但我不介意另外使用其他东西),我有一个指向PDF文件的菜单项(用户下载)

该PDF文件存在于该网站支持的4种语言中,但我找不到为每个语言自定义菜单项href的方法


如何实现这一点?

因为您使用的是转置,所以可以通过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>

//这将获取用户所在的当前页面的语言
var language=document.querySelector('.tr_active').children[0].getAttribute(“title”);
//#pdfLink是页面中ID为pdfLink的锚定标记,您打算对其进行自定义
var pdfLink=document.querySelector('#pdfLink');
如果(语言==“英语”)
{
pdfLink.href=”http://path/to/englishFile.pdf";
}
else if(语言==“法语”)
{
pdfLink.href=”http://path/to/frenchFile.pdf";
}
否则,如果…(其他语言条件)。。。

如果我正确理解您的问题,您(或您的用户)可以将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 {} ?> 

在此处显示英文PDF
在此处显示意大利语PDF
ICL\u language\u code
位基本上获取当前正在查看的语言

你明白我的意思了吗

链接到他们的网站:

链接到文档:


谢谢。

您可以将href设置为一个php文件,然后重定向到正确的pdf文件。编写代码是我最后的选择。我很确定我不是第一个需要这个功能的Wordpress用户。pdf文件的名称是什么?你的问题是你不能?将4种不同版本的pdf(取决于语言)链接到一个
下载
链接?问题是transposh不允许自定义href,而它允许为每种(当前选定的)语言自定义文本。我感谢您的努力,但这会扼杀“用户友好型CMS”的概念。本网站旨在为那些对编码一无所知的人定制。对于非编码答案,你在或处会有更好的运气。