WordPress.org

Support

Support » Plugins and Hacks » Polylang » [Resolved] Conditional language switcher display

[Resolved] Conditional language switcher display

  • I am trying to get the language switcher to be displayed in my template only if a translation exist. The default behaviour where the home page

    Reading through the support articles, I found references and sample code about the pll_the_language_link filter, but being the php dummy that I am, I struggle to find or write a function that I can apply a test on from my template to know whether a translation exists, and call pll_the_languages() depending on the result. Did not find any in api.php at least.

    Any simple solution ?

    Help much appreciated !

    http://wordpress.org/extend/plugins/polylang/

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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; ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Resolved] Conditional language switcher display’ is closed to new replies.
Skip to toolbar