WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] Conditional language switcher display (7 posts)

  1. zeshark
    Member
    Posted 1 year ago #

    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/

  2. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    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>
  3. zeshark
    Member
    Posted 1 year ago #

    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; ?>
  4. maxoud
    Member
    Posted 1 year ago #

    <?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; ?>
  5. zeshark
    Member
    Posted 1 year ago #

    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.

  6. maxoud
    Member
    Posted 1 year ago #

    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
    ) );
  7. zeshark
    Member
    Posted 1 year ago #

    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; ?>

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic