• Resolved Genevieve

    (@sweetgenevieve)


    I’m trying to use <?php pll_the_languages(array('dropdown'=>1)); ?> to display a language switcher drop-down in my theme, and I found the widget’s drop-down action code in polylang/include/widget.php, but I can’t get it to work. The action code kills the whole page. This is how I’m using it:

    <?php pll_the_languages(array('dropdown'=>1)); ?>
    <?php foreach ($polylang->get_languages_list() as $language) {
    				$url = $force_home || ($url = $polylang->get_translation_url($language)) == null ? $polylang->get_home_url($language) : $url;
    				$urls[] = '"'.esc_js($language->slug).'":"'.esc_url($url).'"';
    			}
    
    			$urls = implode(',', $urls);
    
    			$js = "
    				<script type='text/javascript'>
    					//<![CDATA[
    					var urls = {{$urls}};
    					var d = document.getElementById('lang_choice');
    					d.onchange = function() {
    						for (var i in urls) {
    							if (this.value == i)
    								location.href = urls[i];
    						}
    					}
    					//]]>
    				</script>";
    
    			echo $js;
    		} ?>

    Any help would be greatly appreciated!

    https://www.ads-software.com/extend/plugins/polylang/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Chouby

    (@chouby)

    You must add

    global $polylang;

    before your foreach
    I also believe that you must remove the last }

    Thread Starter Genevieve

    (@sweetgenevieve)

    That worked! Thanks so much!

    So, for those looking to do the same thing as me, this is what my final code looks like:

    <div><?php if(function_exists('pll_the_languages')) {
    		pll_the_languages(array('dropdown'=>1));
    	} ?></div>
    	<?php global $polylang;
    		foreach ($polylang->get_languages_list() as $language) {
    				$url = $force_home || ($url = $polylang->get_translation_url($language)) == null ? $polylang->get_home_url($language) : $url;
    				$urls[] = '"'.esc_js($language->slug).'":"'.esc_url($url).'"';
    			}
    
    			$urls = implode(',', $urls);
    
    			$js = "
    				<script type='text/javascript'>
    					//<![CDATA[
    					var urls = {{$urls}};
    					var d = document.getElementById('lang_choice');
    					d.onchange = function() {
    						for (var i in urls) {
    							if (this.value == i)
    								location.href = urls[i];
    						}
    					}
    					//]]>
    				</script>";
    
    			echo $js;
    	?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Help with Drop-down action’ is closed to new replies.