michaelwilhelmsen
2/3/2017 - 8:57 AM

Custom WPML Language Selector

Custom WPML Language Selector

/* WPML */
function sp_language_selector_lang_code(){
  $languages = icl_get_languages('skip_missing=0&orderby=code');
  $i = 0;
  $len = count($languages);

  // If there is one or more languages
  if(!empty($languages)){

    echo '<div id="lang_sel_list_custom">';
    echo '<span class="globe"><i class="fa fa-globe" aria-hidden="true"></i></span>';

    // List languages
    foreach($languages as $l){

      // increase count
      $i++;

      // Give each listing a wrapper
      if(!$l['active']) {
        echo '<a href="'.$l['url'].'">';
      } else {
        echo '<span class="active-lang">';
      }

      // Add language conditionals here
      if ($l['language_code'] == 'nb') {
        echo __('Norway', 'screenpartner');
      } else if ($l['language_code'] == 'sv') {
        echo __('Sweden', 'screenpartner');
      }

      // Close listing wrappers
      if(!$l['active']) {
        echo '</a>';
      } else {
        echo '</span>';
      }

      // add seperator for every listing except last one
      if ($i < $len) {
        echo '<span class="lang-seperator">|</span>';
      }
    }

    echo '</div>';
  }
}
add_shortcode('sp_language_selector', 'sp_language_selector_lang_code');