Use a Different Menu Per Language in WPML
Shows a completely different menu in a theme location for each WPML language, for sites whose language versions have different structures rather than translated copies.
Opens WPIDE on
Code
$wpide_menus = array( // Theme location => array( language code => menu ID ).
'primary' => array(
// 'fr' => 12,
// 'de' => 15,
),
);
add_filter( 'wp_nav_menu_args', function ( $args ) use ( $wpide_menus ) {
if ( ! defined( 'ICL_SITEPRESS_VERSION' ) || empty( $args['theme_location'] ) || ! isset( $wpide_menus[ $args['theme_location'] ] ) ) {
return $args;
}
$lang = apply_filters( 'wpml_current_language', null );
$map = $wpide_menus[ $args['theme_location'] ];
if ( $lang && ! empty( $map[ $lang ] ) && wp_get_nav_menu_object( (int) $map[ $lang ] ) ) {
$args['menu'] = (int) $map[ $lang ]; // An explicit menu wins over the location's assigned menu.
}
return $args;
} );
Notes
Map location => language => menu ID (find IDs in Appearance > Menus, in the URL). Languages not listed keep WPML's usual translated menu.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.