Advertisement
Guest User

polylang-lang-list

a guest
Nov 10th, 2011
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.80 KB | None | 0 0
  1.     // Template tag: Displays links to the current page in other languages
  2.     // Usage: do_action('the_languages');
  3.     function the_languages($args = '') {
  4.         $defaults = array(
  5.             'dropdown' => 0, // display as list and not as dropdown
  6.             'show_names' => 1, // show language names
  7.             'show_flags' => 0, // don't show flags
  8.             'hide_if_empty' => 1 // hides languages with no posts (or pages)
  9.         );
  10.  
  11.         extract(wp_parse_args($args, $defaults));
  12.  
  13.         $listlanguages = $this->get_languages_list($hide_if_empty);
  14.         $output = $dropdown ? '<select name="lang_choice" id="lang_choice" onchange="window.location.href=this.options[this.selectedIndex].value">' : "<ul>\n";
  15.         foreach ($listlanguages as $language) {
  16.             $url = $this->get_translation_url($language);
  17.             $url = isset($url) ? $url : $this->get_home_url($language); // if the page is not translated, link to the home page
  18.  
  19.             $class = 'lang-item lang-item-'.esc_attr($language->term_id);
  20.             if ($language->slug == $this->curlang->slug)
  21.                 $class .= ' current-lang';
  22.  
  23.             $flag = $show_flags &&
  24.                 file_exists(POLYLANG_DIR.($file = '/local_flags/'.$language->description.'.png')) ||
  25.                 file_exists(POLYLANG_DIR.($file = '/flags/'.$language->description.'.png')) ?
  26.                 '<img src="'.WP_PLUGIN_URL.'/polylang'.$file.'" alt="'.$language->name.'" />' : '';
  27.  
  28.             $name = $show_names || !$show_flags ? esc_attr($language->name) : '';
  29.  
  30.             $output .= $dropdown ?
  31.                 sprintf(
  32.                     "<option value='%s'%s>%s</option>\n",
  33.                     $url,
  34.                     $language->slug == $this->curlang->slug ? ' selected="selected"' : '',
  35.                     $name // FIXME flag does not work for the dropdown list
  36.                 ) :
  37.                 '<li class="'.$class.'"><a href="'.$url.'">'.($show_flags && $show_names ? $flag.'&nbsp;'.$name : $name)."</a></li>\n";
  38.         }
  39.         $output .= $dropdown ? '</select>' : "</ul>\n";
  40.         echo $output;
  41.     }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement