Menu add language switcher
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
// Filter wp_nav_menu() to add additional links and other output add_filter('wp_nav_menu_items', 'new_nav_menu_items', 10, 2); function new_nav_menu_items($items, $args) { if (function_exists('icl_get_languages')) { $languages = icl_get_languages('skip_missing=0'); if( ($args->theme_location == 'primary-menu') || ($args->theme_location == 'main-bottom-menu-1-fr') ){ if(!empty($languages)){ foreach($languages as $l){ if(!$l['active']){ // flag with translated name $items = $items . '<li class="menu-item"><a href="' . $l['url'] . '" class="menu-item-link">' . $l['language_code'] . '</a></li>'; } } } } } return $items; } OR // DEFINE POSITION OF THE ADDING MENU ITEMS // Filter wp_nav_menu() to add additional links and other output add_filter('wp_nav_menu_items', 'new_nav_menu_items', 10, 2); function new_nav_menu_items($items, $args) { if (function_exists('icl_get_languages')) { $languages = icl_get_languages('skip_missing=0'); if( ($args->theme_location == 'menu-extra-top')){ if(!empty($languages)){ foreach($languages as $l){ if(!$l['active']){ // flag with translated name //$items = $items . '<li class="menu-item"><a href="' . $l['url'] . '" class="menu-item-link">' . $l['native_name'] . '</a></li>'; $items_array = array(); while ( false !== ( $item_pos = strpos ( $items, '<li', 3 ) ) ){ $items_array[] = substr($items, 0, $item_pos); $items = substr($items, $item_pos); } $items_array[] = $items; array_splice($items_array, 4, 0, '<li class="menu-item"><a href="' . $l['url'] . '" class="menu-item-link">' . $l['native_name'] . '</a></li>'); // insert custom item after 2nd one $items = implode('', $items_array); } } } } } return $items; } |
Array data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
Array ( [0] => Array ( [id] => 1 [active] => 1 [native_name] => English [missing] => 0 [translated_name] => English [language_code] => en [country_flag_url] => http://yourdomain/wpmlpath/res/flags/en.png [url] => http://yourdomain/about ) [1] => Array ( [id] => 4 [active] => 0 [native_name] => Français [missing] => 0 [translated_name] => French [language_code] => fr [country_flag_url] => http://yourdomain/wpmlpath/res/flags/fr.png [url] => http://yourdomain/fr/a-propos ) [2] => Array ( [id] => 27 [active] => 0 [native_name] => Italiano [missing] => 0 [translated_name] => Italian [language_code] => it [country_flag_url] => http://yourdomain/wpmlpath/res/flags/it.png [url] => http://yourdomain/it/circa ) ) |
Add ?_temp_menu_locations to your URL after putting this in your functions.php to know which theme location menu exist
1 2 3 4 5 6 7 8 9 10 11 12 |
if (isset($_GET['_temp_menu_locations'])) { add_action('wp', 'wpml_get_menues_locations'); function wpml_get_menues_locations() { $locations = get_nav_menu_locations(); if (is_array($locations)) { foreach ($locations as $locname => $menu_id) { echo $locname . '<br />'; } } die; } } |
Leave a Comment