php 使用polylang时如何确定wordpress页面的当前语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29118772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine the current language of a wordpress page when using polylang?
提问by Felix TheCat
I search for a variable that outputs the currently used language of the polylang plugin. Something like:
我搜索输出 polylang 插件当前使用的语言的变量。就像是:
if($curlang == "en") {
...
}
回答by Felix TheCat
The solution was simply:
解决方法很简单:
if (get_locale() == 'en_GB') {
回答by Shoaib Bazmi
pll_current_language
Returns the current language
Usage:
pll_current_language( $value );
- $value => (optional) either nameor localeor slug, defaults to slug
returns either the full name, or the WordPress locale (just as the WordPress core function ‘get_locale' or the slug ( 2-letters code) of the current language.
pll_current_language
返回当前语言
用法:
pll_current_language( $value );
- $value => (可选) name或locale或slug,默认为slug
返回全名或 WordPress 语言环境(就像 WordPress 核心函数“get_locale”或当前语言的 slug(2 个字母的代码)一样。
回答by Ingratus
To show current language, you can use:
要显示当前语言,您可以使用:
<?php echo $lang=get_bloginfo("language"); ?>
Plain and simple
干净利落
回答by mvbrakel
This plugin is documented rather good in https://polylang.wordpress.com/documentation.
这个插件在https://polylang.wordpress.com/documentation 中记录得相当好。
Switching post language
切换帖子语言
The developers documentation states the following logic as a means to generate URL's for different translations of the same post
开发人员文档将以下逻辑声明为一种为同一帖子的不同翻译生成 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; ?>
If you want more influence on what is rendered, inspet pll_the_languages
function and copy it's behaviour to your own output implementation
如果您想对呈现的内容产生更多影响,请检查pll_the_languages
函数并将其行为复制到您自己的输出实现
Switching site language
切换网站语言
As you want buttons to switch language, this page: https://polylang.wordpress.com/documentation/frequently-asked-questions/the-language-switcher/will give you the required info.
当您希望按钮切换语言时,此页面:https: //polylang.wordpress.com/documentation/frequently-asked-questions/the-language-switcher/将为您提供所需的信息。
An implementation example:
一个实现示例:
<ul><?php pll_the_languages();?></ul>
Then style with CSS to create buttons, flags or whatever you want. It is also possible to use a widget for this, provided by te plugin
然后使用 CSS 设置样式以创建按钮、标志或任何您想要的内容。也可以为此使用一个小部件,由 te 插件提供
Getting current language
获取当前语言
All plugins functions are explained here: https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/
这里解释了所有插件功能:https: //polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/
In this case use:
在这种情况下使用:
pll_current_language();
回答by MD. Shafayatul Haque
Simple:
简单的:
if(pll_current_language() == 'en'){
//do your work here
}
回答by Diana
I use something like this:
我使用这样的东西:
<?php
$lang = get_bloginfo("language");
if ($lang == 'fr-FR') : ?>
<p>Bienvenue!</p>
<?php endif; ?>
回答by Purnendu Sarkar
<?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';
}
?>