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

I am looking for a variable that displays the current polylang plugin language used. Something like:

if($curlang == "en") { ... } 
+25
php hyperlink wordpress
source share
7 answers

The solution was simple:

 if (get_locale() == 'en_GB') { 
+44
source share

To show the current language, you can use:

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

Plain and simple

+15
source share

pll_current_language

Returns current language

Using:

 pll_current_language( $value ); 
  • $ value => (optional) either name, or locale, or slug, default is slug

returns either the full name or the WordPress locale (just like the main WordPress' Get_locale function or slug (two-letter code) of the current language.

+13
source share

This plugin is pretty well documented at https://polylang.wordpress.com/documentation .

Switching the message language

The developer documentation states the following logic as a means to create URLs 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 to influence what is visualized more, attach the pll_the_languages function and copy its behavior into your own output implementation

Website language switching

If you want the buttons to switch the language, this page: https://polylang.wordpress.com/documentation/frequently-asked-questions/the-language-switcher/ will provide you with the necessary information.

Implementation Example:

 <ul><?php pll_the_languages();?></ul> 

Then style with CSS to create buttons, flags, or whatever. You can also use the widget provided by the te plugin.

Getting current language

All plugin functions are described here: https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/

In this case, use:

 pll_current_language(); 
+9
source share

Plain:

 if(pll_current_language() == 'en'){ //do your work here } 
+4
source share

I am using something like this:

 <?php $lang = get_bloginfo("language"); if ($lang == 'fr-FR') : ?> <p>Bienvenue!</p> <?php endif; ?> 
+2
source share
 <?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'; } ?> 
+1
source share

All Articles