htdat

https://wpml.org/?p=643290

Jul 7th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.49 KB | None | 0 0
  1. /**
  2.  * Filter wp_nav_menu() to add secondary language first followed by
  3.  * current language in drop down. Flags only
  4.  *
  5.  * Note: Works for 2 active languages only
  6.  * Additional note: Take the time to study the output code.
  7.  */
  8. add_filter('wp_nav_menu_items', 'wpml_flag_nav_menu_items', 10, 2);
  9. function wpml_flag_nav_menu_items($items, $args) {
  10. // uncomment this to find your theme's menu location
  11. //echo "args: <pre>"; print_r($args); echo "</pre>";
  12.  
  13.     // adjust $args->theme_location == 'primary' in the conditional below to specify the menu location
  14.     if(function_exists('icl_get_languages') && $args->theme_location == 'primary'){
  15.         global $sitepress_settings, $sitepress;
  16.         // get active languages. Available params here:
  17.             // http://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/
  18.         $languages = $sitepress->get_ls_languages('skip_missing=0');
  19.        
  20.         if(!empty($languages)){
  21.             // collect secondary languages in array
  22.             $secondary_languages = array();
  23.             foreach($languages as $code => $lang){
  24.                 if(!$lang['active']){
  25.                     $secondary_languages[] = $lang;
  26.                 }
  27.             }
  28.         }
  29.  
  30.         $items .= '<li class="menu-item menu-item-language menu-item-language-current">';
  31.             if(isset($args->before)){
  32.                 $items .= $args->before;
  33.             }                                
  34.             $items .= '<a href="' . $secondary_languages[0]['url'] . '">';
  35.             if(isset($args->link_before)){
  36.                 $items .= $args->link_before;
  37.             }
  38.             $items .= '<img class="iclflag" src="' . $secondary_languages[0]['country_flag_url'] . '" width="18" height="12" alt="' . $secondary_languages[0]['translated_name'] . '" />'. $secondary_languages[0]['native_name'];
  39.            
  40.             if(isset($args->link_after)){
  41.                 $items .= $args->link_after;
  42.             }                                
  43.             $items .= '</a>';
  44.             if(isset($args->after)){
  45.                 $items .= $args->after;
  46.             }                                            
  47.            
  48.             unset($languages[$secondary_languages[0]['language_code']]);
  49.             if(!empty($languages)){
  50.                 $items .= '<ul class="sub-menu submenu-languages">';
  51.                 foreach($languages as $code => $lang){
  52.                     $items .= '
  53.                     <li class="menu-item menu-item-language menu-item-language-current">
  54.                         <a href="' . $lang['url'] . '">
  55.                             <img class="iclflag" src="' . $lang['country_flag_url'] . '" width="18" height="12" alt="' . $lang['translated_name'] . '" />' . $lang['native_name'] . '
  56.                         </a>
  57.                     </li>';
  58.                 }
  59.                 $items .= '</ul>';
  60.             }
  61.         $items .= '</li>';
  62.     }
  63.  
  64. return $items;
  65. }
Add Comment
Please, Sign In to add comment