我正在编写404自定义页面,一切都很好,但我使用的是WPML,因此我正在本地化我的字符串。
这是我代码的一部分:
<h4><?php _e('You still can\'t find anything?', 'kslang'); ?></h4>
<h5><?php _e('Well, then you probably should give us a wake up call...', 'kslang'); "\n"?></h5>
<h5><?php _e('but be aware who you\'re waking up!', 'kslang');?></h5>
<h5><?php _e('You\'re sure? Well, then...', 'kslang'); ?></h5>
<form>
<input type="button" value="<?php _e('Contact Us!', 'kslang'); ?>" onClick="window.location.href='http://example.com/contact-us-today'">
</form>
<h5><?php _e('or try the Site Map Below. You\'re also welcome to check out our related projects!', 'kslang'); ?></h5>现在问题是,我
"window.location.href='http://example.com/contact-us-today'"不是重定向到相关语言。
我想我可以简单地将href链接包装在get文本中,就像by按钮文本和其他字符串一样。
这是行不通的,显然事情更复杂。我需要使用if/else语句吗?
有没有人知道如何重定向到正确的语言?(我尝试使用_e来翻译字符串转换器中的href链接,但这并不适用于_e,因为插入php片段会破坏我的站点。)
希望有人能给出建议..。
发布于 2015-01-22 11:34:02
要使用window.location.href重定向到适当的语言版本,您需要将该语言包含在您的URL中.例如,对于法语,重定向到"http://www.example.com/fr/contact-us-today“(域后面有"fr”)。您可以通过在PHP中检测当前的WPML语言并将其作为JavaScript变量回显,从而动态地做到这一点,如下所示:
//Get current WPML language
global $sitepress;
$language = $sitepress->get_current_language();
//Echo as JS variable
echo "<script>var lang = '".$language."';</script>"然后,您可以这样重定向:
window.location.href='http://www.example.com/'+lang+'/contact-us-today';注意:本例假设您正在为您的不同语言使用语言文件夹。如果您正在使用子域或语言查询参数,则需要相应地进行调整。
https://stackoverflow.com/questions/27455710
复制相似问题