Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 1 reply, has 2 voices.

Last updated by Sumit 11 months, 3 weeks ago.

Author Posts
February 7, 2024 at 10:52 am #15276130

mattW-28

Tell us what you are trying to do?
I'd like the navigation menus to programmatically show the language variant of a link, otherwise fall back to the default language.

Is there any documentation that you are following?
I can't find any.

Is there a similar example that we can see?
hidden link

What is the link to your site?
hidden link

For context, the client only wants about 20% of the website translated into different languages.

February 7, 2024 at 4:19 pm #15277776

Sumit
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi,

Thank you for contacting the support forum.
Before your ticket is assigned to one of my colleagues, please allow me to walk you through some initial debugging steps. This will help speed up the support process.

The current behavior of WPML is as soon as you translate the content the translated links are shown in the menu and untranslated content is skipped. This is what happening currently on the site.

The other option is to display the original menu as it is without translating them at all. This is not an official feature but there is a workaround. You can add this code to the theme's functions.php

$locations_to_omit = ['primary']; //Append menu locations here. e.g. ['primary', 'footer']
add_filter('wp_nav_menu_args', function ($args) use ($locations_to_omit) {
	if (isset($args['theme_location']) && in_array($args['theme_location'], $locations_to_omit)) {
		$current_lang = apply_filters( 'wpml_current_language', null );
		do_action('wpml_switch_language', apply_filters( 'wpml_default_language', null ));
		
		add_filter('wp_nav_menu', function ($nav_menu) use ($current_lang){
			do_action('wpml_switch_language', $current_lang);
			return $nav_menu;
		}, 99);
	}
	
	return $args;
}, 1);

In the code you need to add the menu location name here $locations_to_omit = ['primary'];

The third and last option is to translate the menu and add whatever link you like to add from the primary language or secondary language. please see https://wpml.org/documentation/getting-started-guide/translating-menus/#fully-custom-create-menus-manually

I hope it helps.
Thanks

The topic ‘[Closed] WP Nav Menus’ is closed to new replies.