• Resolved janfry

    (@janfry)


    Hello, thanks for developing Polylang.

    I am having a weird issue when using pll_the_languages function. The list of languages it outputs is duplicated, there are 2 flags/strings for each language.

    This is the code I’m using in functions.php:

    /* Add Polylang-plugin language switch to header*/
    
    function add_language_switch() {
    
    // check existance of pll_the_languages to prevent wordpress for breaking when Polylang is not available (for example when being updated)
      if (function_exists('pll_the_languages')) {
        echo '<ul>' . pll_the_languages(array('show_flags'=>1,'show_names'=>0)) . '</ul>';
      } else {
        echo 'Polylang not available';
      }
    }
    
    add_action('mytheme_header','add_language_switch', 30);

    What am I doing wrong?
    Also is this a good way to use Polylang?

    On a side note, when I add polylang switcher to the menu through the wordpress menu settings the languages are not duplicated.

    Thanks in adv.

    https://wordpress.org/plugins/polylang/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter janfry

    (@janfry)

    To help understand better, here is the outputted HTML (there are three languages flags, each showing twice):

    <li class="lang-item lang-item-4 lang-item-it current-lang"><a hreflang="it" href="http://mywebsite.com/it/"><img src="http://mywebsite.com/wp-content/plugins/polylang/flags/it_IT.png" title="Italiano" alt="Italiano"></a></li>
    
    <li class="lang-item lang-item-7 lang-item-en"><a hreflang="en" href="http://mywebsite.com/en/"><img src="http://mywebsite.com/wp-content/plugins/polylang/flags/en_US.png" title="English" alt="English"></a></li>
    
    <li class="lang-item lang-item-11 lang-item-es"><a hreflang="es" href="http://mywebsite.com/es/"><img src="http://mywebsite.com/wp-content/plugins/polylang/flags/es_ES.png" title="Español" alt="Español"></a></li>
    
    <-- duplication occurs here but notice how the following languages are enclosed in a <ul> tag, which is exactly how I coded it in functions.php, so it seems that the dupes are those above -->
    
    <ul>
    <li class="lang-item lang-item-4 lang-item-it current-lang"><a hreflang="it" href="http://mywebsite.com/it/"><img src="http://mywebsite.com/wp-content/plugins/polylang/flags/it_IT.png" title="Italiano" alt="Italiano"></a></li>
    
    <li class="lang-item lang-item-7 lang-item-en"><a hreflang="en" href="http://mywebsite.com/en/"><img src="http://mywebsite.com/wp-content/plugins/polylang/flags/en_US.png" title="English" alt="English"></a></li>
    <li class="lang-item lang-item-11 lang-item-es"><a hreflang="es" href="http://mywebsite.com/es/"><img src="http://mywebsite.com/wp-content/plugins/polylang/flags/es_ES.png" title="Español" alt="Español"></a></li>
    
    </ul>

    If anybody has a clue please help… I really have no clue how this is happening and how I can fix it.
    Thanks

    Thread Starter janfry

    (@janfry)

    Duplicates don’t show up if I use either

    pll_the_languages(array('raw'=>1))

    or

    pll_languages_list($args)

    But I’d still prefer to go with pll_the_languages() because I don’t have to work on arrays and build the HTML.

    Can someone help?

    Plugin Author Chouby

    (@chouby)

    Hi!

    That’s because you use ‘echo’ and ‘pll_the_languages’ already uses it (by default). You can use the parameter 'echo' => 0 to avoid this.

    Thread Starter janfry

    (@janfry)

    Ouch!
    I should have inspected polylang code a bit better! Or I could have simply tried without calling echo on it, which I had planned, among all other tries, but it somehow slipped out of my brain… and I ended up building the PHP/HTML myself.. oh well.. let’s take it as a PHP brush up 🙂

    Cheers and thanks for your support though! Appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘duplicated languages string/flags with pll_the_languages’ is closed to new replies.