Skip to content

Instantly share code, notes, and snippets.

@nicomollet
Last active January 17, 2024 11:25
  • Star 17 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nicomollet/47ba9808f3187c9f1568d8f7c4355b54 to your computer and use it in GitHub Desktop.
Polylang shortcode for lang switcher
<?php
/**
* Polylang Shortcode - https://wordpress.org/plugins/polylang/
* Add this code in your functions.php
* Put shortcode [polylang_langswitcher] to post/page for display flags
*
* @return string
*/
function custom_polylang_langswitcher() {
$output = '';
if ( function_exists( 'pll_the_languages' ) ) {
$args = [
'show_flags' => 0,
'show_names' => 1,
'echo' => 0,
];
$output = '<ul class="polylang_langswitcher">'.pll_the_languages( $args ). '</ul>';
}
return $output;
}
add_shortcode( 'polylang_langswitcher', 'custom_polylang_langswitcher' );
@vieirabmp
Copy link

Hello, how can I change the way the flags are displayed?
Capturar
Don't want the marks.
Thank you!

@cgdmohamed
Copy link

Hello, how can I change the way the flags are displayed?
Capturar
Don't want the marks.
Thank you!

.lang-item {
list-style: none;
}

@sushinger
Copy link

Wonderful, thanks 🙏 Is it possible to only display flags of the non-active languages? How?

@davy440
Copy link

davy440 commented Jan 23, 2022

@sushinger just the argument hide_if_empty => 0 to $args.

New array should be -

$args   = [
		'show_flags' => 0,
                'hide_if_empty' => 0,
		'show_names' => 1,
		'echo'       => 0,
	];

Hope that helps!

@elbsegler63
Copy link

Is there a way to hide the currently displayed language?

@sanchawebo
Copy link

@elbsegler63 Use the hide_current argument and set it to 1.

Here is the complete arguments list:

/**
* @param array     $args {
*   Optional array of arguments.
*
*   @type int      $dropdown               The list is displayed as dropdown if set, defaults to 0.
*   @type int      $echo                   Echoes the list if set to 1, defaults to 1.
*   @type int      $hide_if_empty          Hides languages with no posts ( or pages ) if set to 1, defaults to 1.
*   @type int      $show_flags             Displays flags if set to 1, defaults to 0.
*   @type int      $show_names             Shows language names if set to 1, defaults to 1.
*   @type string   $display_names_as       Whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name.
*   @type int      $force_home             Will always link to home in translated language if set to 1, defaults to 0.
*   @type int      $hide_if_no_translation Hides the link if there is no translation if set to 1, defaults to 0.
*   @type int      $hide_current           Hides the current language if set to 1, defaults to 0.
*   @type int      $post_id                Returns links to the translations of the post defined by post_id if set, defaults not set.
*   @type int      $raw                    Return a raw array instead of html markup if set to 1, defaults to 0.
*   @type string   $item_spacing           Whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to 'preserve'.
*   @type int      $admin_render           Allows to force the current language code in an admin context if set, default to 0. Need to set the admin_current_lang argument below.
*   @type string   $admin_current_lang     The current language code in an admin context. Need to set the admin_render to 1, defaults not set.
*   @type string[] $classes                A list of CSS classes to set to each elements outputted.
*   @type string[] $link_classes           A list of CSS classes to set to each link outputted.
* }
*/

@descrop
Copy link

descrop commented Apr 3, 2023

CSS para mostrar las banderas en línea y sin viñetas, el relleno es solo para agregar espacio entre los elementos.

CSS to display the flags inline and without bullets, the padding is just to add space between elements.

.lang-item{
display:inline;
padding-left:5px;
list-style:none;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment