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:
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.
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.
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:
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!
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!
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!