Advertisement
htdat

WordPress menu filter - secondary lang first - no drop-down

Aug 12th, 2015
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.67 KB | None | 0 0
  1. /** For this specific need: https://wpml.org/forums/topic/i-only-want-the-other-language-appear-on-the-language-switcher-instead-both/#post-677557
  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. add_filter('wp_nav_menu_items', 'wpml_flag_nav_menu_items', 10, 2);
  11. function wpml_flag_nav_menu_items($items, $args) {
  12. // uncomment this to find your theme's menu location
  13. //echo "args: <pre>"; print_r($args); echo "</pre>";
  14.  
  15.     // adjust $args->theme_location == 'primary' in the conditional below to specify the menu location
  16.     if(function_exists('icl_get_languages') && $args->theme_location == 'primary'){
  17.         global $sitepress_settings, $sitepress;
  18.         // get active languages. Available params here:
  19.             // http://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/
  20.         $languages = $sitepress->get_ls_languages();
  21.        
  22.         if(!empty($languages)){
  23.             // collect secondary languages in array
  24.             $secondary_languages = array();
  25.             foreach($languages as $code => $lang){
  26.                 if(!$lang['active']){
  27.                     $secondary_languages[] = $lang;
  28.                 }
  29.             }
  30.         }
  31.  
  32.         $items .= '<li class="menu-item menu-item-language menu-item-language-current">';
  33.             if(isset($args->before)){
  34.                 $items .= $args->before;
  35.             }                                
  36.             $items .= '<a href="' . $secondary_languages[0]['url'] . '">';
  37.             if(isset($args->link_before)){
  38.                 $items .= $args->link_before;
  39.             }
  40.             $items .= '<img class="iclflag" src="' . $secondary_languages[0]['country_flag_url'] . '" width="18" height="12" alt="' . $secondary_languages[0]['translated_name'] . '" />';
  41.            
  42.             if(isset($args->link_after)){
  43.                 $items .= $args->link_after;
  44.             }                                
  45.             $items .= '</a>';
  46.             if(isset($args->after)){
  47.                 $items .= $args->after;
  48.             }                                            
  49.            
  50.             unset($languages[$secondary_languages[0]['language_code']]);
  51.         /*  if(!empty($languages)){
  52.                 $items .= '<ul class="sub-menu submenu-languages">';
  53.                 foreach($languages as $code => $lang){
  54.                     $items .= '
  55.                     <li class="menu-item menu-item-language menu-item-language-current">
  56.                         <a href="' . $lang['url'] . '">
  57.                             <img class="iclflag" src="' . $lang['country_flag_url'] . '" width="18" height="12" alt="' . $lang['translated_name'] . '" />
  58.                         </a>
  59.                     </li>';
  60.                 }
  61.                 $items .= '</ul>';
  62.             } */
  63.         $items .= '</li>';
  64.     }
  65.  
  66. return $items;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement