Polylang language switcher - show only language codes

8.3k Views Asked by At

I created a menu inside a theme I'm creating and have added a language switcher that comes with Polylang.

On the fronted the switcher shows up as it should, but it shows the full name of the language (eg. English, Deutsch,...). Instead of full names, I would like country codes to be printed.

Now:

enter image description here

Wanted result:

enter image description here

2

There are 2 best solutions below

0
On

You achieve this in two ways:

  1. adding 'display_names_as'=>'slug' to the args

    $args = array('show_flags' => 0, 'show_names' => 0, 'hide_current' => false,'dropdown' => 1,'display_names_as'=>'slug'); pll_the_languages($args);

Or

  1. Polylang hook Just add this snippet in your function.php file:

    add_filter( 'pll_the_languages_args', function( $args ) { $args['display_names_as'] = 'slug'; return $args; } );

0
On

This is my code,add the code in your file functions.php, and then add shortcode in the place where you want to show the list of languages it shows the languages in the form of a country flag, you can show what you want just remove a comment on a var_dump.then edit it as you like

<?php

/**
 * Put shortcode [polylang_langswitcher] to post/page for display flags
 */
function custom_polylang_langswitcher() {
    $langs_array = pll_the_languages( array( 'dropdown' => 1, 'hide_current' => 0, 'raw' => 1 ) );
    //var_dump($langs_array);
    if ($langs_array) : ?>
    <div class="drop-block lang">
        <?php foreach ($langs_array as $lang) : ?>
        <a href="<?php echo $lang['url']; ?>" class="drop-block__link icon-<?php echo $lang['slug']; ?>">
            <img width="32" height="32" src="<?php echo $lang['flag']; ?>" alt="<?php echo $lang['slug']; ?>" />
        </a>
        <?php endforeach; ?>
    </div>
    <?php endif; 
}

add_shortcode( 'polylang_langswitcher', 'custom_polylang_langswitcher' );

?>