Advertisement
htdat

Show only other languages - WPML - new filter

Dec 10th, 2015
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. // Filter wp_nav_menu() to add additional links and other output
  2. // Show only other language in language switcher
  3. // Use the new filter: https://wpml.org/wpml-hook/wpml_active_languages-2/
  4. add_filter('wp_nav_menu_items', 'new_nav_menu_items', 10, 2);
  5. function new_nav_menu_items($items, $args) {
  6.     // uncomment this to find your theme's menu location
  7.     //echo "args: <pre>"; print_r($args); echo "</pre>";
  8.    
  9.     // get languages
  10.     $languages = apply_filters( 'wpml_active_languages', NULL, 'skip_missing=0' );
  11.    
  12.     // add $args->theme_location == 'primary-menu' in the conditional if we want to specify the menu location.
  13.  
  14.     if ( $languages && $args->theme_location == 'primary') {
  15.  
  16.         if(!empty($languages)){
  17.  
  18.             foreach($languages as $l){
  19.                 if(!$l['active']){
  20.                     // flag with native name
  21.                     $items = $items . '<li class="menu-item"><a href="' . $l['url'] . '"><img src="' . $l['country_flag_url'] . '" height="12" alt="' . $l['language_code'] . '" width="18" /> ' . $l['native_name'] . '</a></li>';
  22.                     //only flag
  23.                     //$items = $items . '<li class="menu-item menu-item-language"><a href="' . $l['url'] . '"><img src="' . $l['country_flag_url'] . '" height="12" alt="' . $l['language_code'] . '" width="18" /></a></li>';
  24.                 }
  25.             }
  26.         }
  27.     }
  28.    
  29.     return $items;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement