我正在寻找一个变量,可以输出Polylang插件当前使用的语言。类似于:
if($curlang == "en") {
...
}
要显示当前语言,您可以使用:
<?php echo $lang=get_bloginfo("language"); ?>
简单明了
pll_current_language
返回当前语言。
用法:
pll_current_language( $value );
返回当前语言的完整名称,或者 WordPress 的语言代码(与 WordPress 核心函数 'get_locale' 相同),或者是当前语言的两个字母的缩写代码。
简单:
if(pll_current_language() == 'en'){
//do your work here
}
这个插件的文档在https://polylang.wordpress.com/documentation中记录得相当不错。
开发者文档说明以下逻辑作为生成同一篇文章不同翻译 URL 的方法。
<?php while ( have_posts() ) : the_post(); ?>
<ul class='translations'><?php pll_the_languages(array('post_id' =>; $post->ID)); ?></ul>
<?php the_content(); ?>
<?php endwhile; ?>
如果您想对渲染内容有更多的影响,请检查pll_the_languages
函数,并将其行为复制到您自己的输出实现中。
由于您希望有按钮来切换语言,因此此页面:https://polylang.wordpress.com/documentation/frequently-asked-questions/the-language-switcher/将为您提供所需的信息。
一个实现示例:
<ul><?php pll_the_languages();?></ul>
使用CSS进行样式设计,创造出按钮、标志或者任何你想要的东西。 此外还可以使用插件提供的小部件来实现。
所有插件函数都在这里解释:https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/
在这种情况下,请使用:
pll_current_language();
我使用类似这样的东西:
<?php
$lang = get_bloginfo("language");
if ($lang == 'fr-FR') : ?>
<p>Bienvenue!</p>
<?php endif; ?>
<?php
$currentpage = $_SERVER['REQUEST_URI'];
$eep=explode('/',$currentpage);
$ln=$eep[1];
if (in_array("en", $eep))
{
$lan='en';
}
if (in_array("es", $eep))
{
$lan='es';
}
?>