Felix TheCat
Felix TheCat

Reputation: 1709

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

I search for a variable that outputs the currently used language of the polylang plugin. Something like:

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

Upvotes: 58

Views: 207410

Answers (7)

Felix TheCat
Felix TheCat

Reputation: 1709

We can use the get_locale function:

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

Upvotes: 111

Purnendu Sarkar
Purnendu Sarkar

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

Diana
Diana

Reputation: 328

I use something like this:

<?php 

$lang = get_bloginfo("language"); 

if ($lang == 'fr-FR') : ?>

   <p>Bienvenue!</p>

<?php endif; ?>

Upvotes: 10

MD. Shafayatul Haque
MD. Shafayatul Haque

Reputation: 998

Simple:

if(pll_current_language() == 'en'){
   //do your work here
}

Upvotes: 20

Shoaib Bazmi
Shoaib Bazmi

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

Ingratus
Ingratus

Reputation: 275

To show current language, you can use:

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

Plain and simple

Upvotes: 27

mvbrakel
mvbrakel

Reputation: 936

This plugin is documented rather good in 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

<?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

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.

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

Getting current language

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

Related Questions