Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client is trying to retrieve the content of translated menus using

wp_get_nav_menu_object()

and

wp_get_nav_menu_items()

functions, but these functions are returning the content in the original language instead of the translated language.

Solution:
We recommend updating WordPress and WPML plugins to the latest versions, ensuring to take a full backup of the website and database before doing so. For more information on handling translations, we suggest checking the following WPML coding API documentation:

To solve the issue, you can use the

wpml_switch_language

hook to switch the language before retrieving the menu objects and items. Here is an example code snippet:

$my_current_lang = apply_filters( 'wpml_current_language', NULL );

// Switch to Italian (IT)
do_action( 'wpml_switch_language', "it" );

// Now retrieve the menu object and items in Italian
$menu = wp_get_nav_menu_object($menu->term_id);
$items = wp_get_nav_menu_items($menu->term_id, array('post_status' => 'any'));

// Switch back to the original language
do_action( 'wpml_switch_language', $my_current_lang );

If this solution does not seem relevant to your issue, please open a new support ticket at WPML Support Forum.

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.

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by Waqas Bin Hasan 1 year, 3 months ago.

Assisted by: Waqas Bin Hasan.

Author Posts
December 24, 2023 at 3:55 pm #15124619

sebastienV-8

Hello,
We want to use these functions:
wp_get_nav_menu_object()
wp_get_nav_menu_items()
Thanks to this post:
https://wpml.org/forums/topic/how-to-get-nav-menu-object-in-second-language/
we are able to get the id of the translated menus. However, if we apply any of these ids to the above WordPress functions, they always return the content of the menu in the original language (French in our case) and not the content of the translated menus (English and Italian). Are there other functions to get the translated contents? Or any other workaround for this question?
Thank you in advance for any hint.
P.S.: we want to use the content of these menus to mirror it on a PHP page outside WordPress.

December 26, 2023 at 7:55 am #15128357

Waqas Bin Hasan
WPML Supporter since 05/2014

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Sebastien,

Thank you for contacting the support.

First of all I'll recommend updating WordPress and WPML plugins to the latest versions. Remember to take full backup of website and database before updating!!

Then I'll recommend checking WPML's coding API for more information:

- https://wpml.org/documentation/support/wpml-coding-api/
- https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/
- https://wpml.org/documentation/support/wpml-coding-api/shortcodes/
- https://wpml.org/faq/how-to-get-current-language-with-wpml/
- https://wpml.org/documentation/support/

Usually you should be able to get the desired results by using https://wpml.org/wpml-hook/wpml_object_id/ in the current language or in another language you specify.

Regards.

December 28, 2023 at 11:39 am #15134337

sebastienV-8

Thank you for yor reply!
We went through all the hooks and did not find any better idea than our initial one, which you also pointed out: using wpml_object_id. Unfortunaltely, wp_get_nav_menu_object($object_id) and wp_get_nav_menu_items($object_id) return the content of the original menu, not the content of the translated one. Even the term_id of the object returned by wp_get_nav_menu_object($object_id) goes back to the original idea:
$menu = wp_get_nav_menu_object('menu-pincipal); // gives $menu->term_id = 66
$idit = apply_filters( 'wpml_object_id', $menu->term_id, 'nav_menu', FALSE, 'it' ); // gives 239
$menuit = wp_get_nav_menu_object($idit); // gives again the menu with term_id 66
We also checked how /wp-admin/nav-menus.php gets the translated content, but we were not able to understand it.
If you have another idea, it would be much appreciated. Otherwise we would have to update the content or our mirrored menu manually each time that the WP menu is modified (which would be possible be less satisfactory).
Best regards!

December 28, 2023 at 1:06 pm #15134848

Waqas Bin Hasan
WPML Supporter since 05/2014

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for the updates.

Can you use this fresh sandbox site hidden link and create the example you're trying?

I'll then be able to work and ask my team for a suggestion.

Please let me know when you're ready.

December 29, 2023 at 12:26 pm #15136649

sebastienV-8

Dear Waqas,
Thank you very much for giving us this opportunity in the sandbox.
I set up 3 languages (FR, EN, IT) and 2 articles with their translations. Then i made a menu showing these 2 articles, plus a personalized link.
To be able to demonstrate the PHP that we want to implement, I edited the footer.php of the theme. As a test environment, it works as you can see from the output just above the standard "Proudly powered by WordPress" in the footer.
Like in our real environment, we are able to get the menu object in the original language, including its ID (here:12). Then we can get its (3) elements, display their title and URL. The next step is to get the ID of one of the translations via wpml_object_id. We get 14. So far, so good. Then we want to get this menu with ID 14, but it fails and gives us again the original (ID 12).
The problem that we encounter is thus reproduced here in the sandbox: we are not able to find the titles of the translated menu.
If you have a solution, it would be much appreciated.
Best regards!

December 29, 2023 at 1:51 pm #15136829

Waqas Bin Hasan
WPML Supporter since 05/2014

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you so much for setting up everything.

I'll soon look into this, by Monday at earliest and 'll return to you accordingly.

Have a great weekend!

January 1, 2024 at 9:33 am #15139289

Waqas Bin Hasan
WPML Supporter since 05/2014

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for your patience and happy new year 🙂

I think I was able to solve this by using wpml_switch_language hook (https://wpml.org/wpml-hook/wpml_switch_language/):

// INSERTED FOR TESTS:
					echo 'TESTS<BR>Original (FR) menu from wp_get_nav_menu_object():<BR>';
				
					$menu = wp_get_nav_menu_object('menu-français');
					echo "<pre>";
					var_dump($menu);
					echo "</pre><hr>";
				
					echo '<BR>Its ID:<BR>'.$menu->term_id;
					echo '<BR>Original (FR) items from wp_get_nav_menu_items():<BR>';
				
					$items = wp_get_nav_menu_items($menu->term_id, array('post_status' => 'any'));
					
					foreach($items as $i)
					  echo $i->title.' -> '.$i->url.'<BR>';
					
					$my_current_lang = apply_filters( 'wpml_current_language', NULL );
				
					echo '<BR>Default ('.$my_current_lang.') menu ID: '.$menu->term_id.'<BR>';
					echo '<BR>Translated (IT) menu ID: ';
					
					//$idtrad = apply_filters( 'wpml_object_id', $menu->term_id, 'nav_menu', FALSE, 'it' );
					//echo ($idtrad??'N/A').'<BR>';
					
					do_action( 'wpml_switch_language', "it" );
				
					echo '<BR>Translated (IT) menu from wp_get_nav_menu_object():<BR>';
					
					$menutrad = wp_get_nav_menu_object($menu->term_id);
					echo "<hr><pre>";
					var_dump($menutrad);
					echo "</pre>";
				
					$items = wp_get_nav_menu_items($menu->term_id, array('post_status' => 'any'));
					
					foreach($items as $i)
					  echo $i->title.' -> '.$i->url.'<BR>';
					
					echo '<BR><hr>';
				
					do_action( 'wpml_switch_language', $my_current_lang );
					$my_current_lang = apply_filters( 'wpml_current_language', NULL );
					echo "Language switched back to: ".$my_current_lang."<br><hr>";

And can be seen at hidden link.

Please check and let me know if that what's you want.

January 2, 2024 at 2:09 pm #15143737

sebastienV-8

Happy New Year to you too!
This is exactly what we need! Indeed, wpml_switch_language allows to get the content of the menu in any language by just switching between languages.
Thank you very much for your help!