Reputation: 1709
I search for a variable that outputs the currently used language of the polylang plugin. Something like:
if($curlang == "en") {
...
}
Upvotes: 58
Views: 207410
Reputation: 1709
We can use the get_locale
function:
if (get_locale() == 'en_GB') {
// drink tea
}
Upvotes: 111
Reputation: 342
<?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';
}
?>
Upvotes: 1
Reputation: 328
I use something like this:
<?php
$lang = get_bloginfo("language");
if ($lang == 'fr-FR') : ?>
<p>Bienvenue!</p>
<?php endif; ?>
Upvotes: 10
Reputation: 998
Simple:
if(pll_current_language() == 'en'){
//do your work here
}
Upvotes: 20
Reputation: 320
pll_current_language
Returns the current language
Usage:
pll_current_language( $value );
- $value => (optional) either name or locale or 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.
Upvotes: 27
Reputation: 275
To show current language, you can use:
<?php echo $lang=get_bloginfo("language"); ?>
Plain and simple
Upvotes: 27
Reputation: 936
This plugin is documented rather good in https://polylang.wordpress.com/documentation.
The developers documentation states the following logic as a means to generate URL's for different translations of the same post
<?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
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.
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
All plugins functions are explained here: https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/
In this case use:
pll_current_language();
Upvotes: 11