如何在使用polylang时确定wordpress页面的当前语言


How to determine the current language of a wordpress page when using polylang?

我搜索一个变量,该变量输出polylang插件当前使用的语言。类似于:

if($curlang == "en") {
  ...
}

我们可以使用get_locale函数:

if (get_locale() == 'en_GB') {
    // drink tea
}

要显示当前语言,可以使用:

 <?php echo $lang=get_bloginfo("language"); ?>

简单明了的

pll_current_language

返回当前语言

用法:

pll_current_language( $value ); 
  • $value=>(可选)名称区域设置,或slug之一,默认为slug

返回全部名称或WordPress的区域设置(就像WordPress的核心功能一样"get_locale"或当前语言的slug(两个字母的代码)。

简单:

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创建按钮、标志或任何您想要的东西。也可以使用由te插件提供的小部件

获取当前语言

这里解释了所有插件的功能: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';
                    }
                ?>