Plugin Author
Chouby
(@chouby)
No need for a lot of PHP code as Polylang already includes this. You can use for example:
<ul><?php pll_the_languages(array('hide_if_no_translation' => 1)); ?></ul>
Thanks a lot for the reply.
Does not do exactly what I need as I would like to display a div only if a translation of a post exists.
<?php if [condition]: ?>
<div id="LanguageSwitcher" class="styleLanguageSwitcher" >
<ul><?php pll_the_languages(array('show_flags'=>1,'show_names'=>1,'force_home'=>0, 'hide_if_no_translation'=>1, 'hide_current'=>1));?></ul>
</div>
<?php endif; ?>
<?php
$switcher = pll_the_languages(array('show_flags'=>1,'show_names'=>1,'force_home'=>0, 'hide_if_no_translation'=>1, 'hide_current'=>1));
if($switcher): ?>
<div id="LanguageSwitcher" class="styleLanguageSwitcher" >
<ul><?php echo $switcher ?></ul>
</div>
<?php endif; ?>
Thanks a lot for your suggestion !
Unfortunately, it does not work.
The language switcher gets displayed at the first function call.
$switcher = pll_the_languages(array('show_flags'=>1,'show_names'=>1,'force_home'=>0, 'hide_if_no_translation'=>1, 'hide_current'=>1));
Also, $switcher seems to be false in all cases.
Oh, sorry, it’s my fault. You need to add echo
argument:
<?php
$switcher = pll_the_languages( array(
'show_flags'=>1,
'show_names'=>1,
'force_home'=>0,
'hide_if_no_translation'=>1,
'hide_current'=>1,
'echo' => 0
) );
It WORKS !
Thanks a million, I really appreciate the help.
For the convenience of others, the final code: this displays in the div only the available language(s), and no div if there is no translation available.
<?php
$switcher = pll_the_languages( array(
'show_flags'=>1,
'show_names'=>1,
'force_home'=>0,
'hide_if_no_translation'=>1,
'hide_current'=>1,
'echo' => 0
) );
if($switcher): ?>
<div id="LanguageSwitcher" class="styleLanguageSwitcher" >
<ul><?php echo $switcher ?></ul>
</div>
<?php endif; ?>