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") {
  ...
}

Related posts

Leave a Reply

7 comments

  1. 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.

  2. 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();
    
  3. <?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';
                        }
                    ?>