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 9 replies, has 2 voices.

Last updated by paulM-12 3 years, 9 months ago.

Assisted by: Riffaz.

Author Posts
July 6, 2020 at 9:42 pm #6533971

paulM-12

HI there,

I want to list all the menus from current language.
For example, I have a English-French bilingual site and I have 3 menus in total.
There are 2 English menus & 1 French menu.

(1) Primary Menu <- EN
(2) Quick Links <- EN
(3) Primary Menu French <- FR

Right now, I am able to list all of the menus, however, I only want the ones from the current language. If I am on the English side, it should list all the English Menus (Primary Menu + Quick Links), or if I am on the French side, it should list all the French Menus (Primary Menu French).

Below is the code that I'm using to list the menus:

$menus = wp_get_nav_menus();
foreach( $menus as $menu){
echo $menu->name."<br>";
}

How can I adjust the code above to display menus only from the current language?

Thanks, Rose

July 7, 2020 at 7:43 am #6535895

Riffaz

Hi,

Thank you for contacting WPML support.

This needs some customization.
I am checking with the team.
I will update you once we find something.

Regards,
Riffaz.

July 7, 2020 at 8:51 am #6536659

Riffaz

Hi,

wp_get_nav_menus() is designed to get all menus.

Pass menus to

wp_nav_menu()

function and WPML will display the translated menu.

Regards,

July 7, 2020 at 1:29 pm #6539539

paulM-12

Hi,

I've tried using ' wp_nav_menu()', but it's not retrieving all the menu from a specific language. I normally use this to display a specific menu, however, I want to retrieve all the menu name/or slug from a certain language. If I am on the English side, it should list all the English menu names (such as Primary Menu, Quick Links), but it isn't.

I've used this code below, and it's returning NULL.
$menus = wp_nav_menu();
var_dump($menus);

Is it possible to retrieve all the menu names/or slugs from the current language?

Thanks!

July 8, 2020 at 9:53 am #6546865

Riffaz

Hi,

We are checking this.
I will get back to you again soon.

Regards,

July 9, 2020 at 8:40 am #6555265

Riffaz

Hi,

Please pass the ID or Name pass menu ID or name https://developer.wordpress.org/reference/functions/wp_nav_menu/ and then check.

let me know the results, please.

Regards,

July 9, 2020 at 1:26 pm #6558655

paulM-12

Hi,

I don't want to display a specific menu, but want to list ALL the menu names from the current language. For example, I have a English-French bilingual site and I have 3 menus in total. There are 2 English menus & 1 French menu.

(1) Primary Menu <- EN
(2) Quick Links <- EN
(3) Primary Menu French <- FR

I want to add a piece of code to the template that list ALL the English menus (the name or slug) when I am on the English side (which are Primary Menu & Quick Links ). When I am on the French side, it should list ALL the French menus (which is Primary Menu French ). However, wp_nav_menu() doesn't do this, as this code displays a specific navigation menu.

Thanks

July 12, 2020 at 6:16 am #6576721

Riffaz

Hi,

I am checking this.
I will get back to you soon.

Regards,

July 13, 2020 at 9:58 am #6582129

Riffaz

Hi,

There is no such function.

All the menus are there you just need to filter the data using WPML API,

Use this wrapper function

function wpml_wp_get_nav_menus() {
    $allMenus = wp_get_nav_menus();
    $menusCurrentLang = [];
    
    if (!empty($allMenus)) {
	foreach ($allMenus as $menu) {
	    $translatedMenuID = apply_filters( 'wpml_object_id', $menu->term_id, 'nav_menu', false );
	    if ($translatedMenuID == $menu->term_id) {
		$menusCurrentLang[$translatedMenuID] = $menu;
	    }
	}
    }
    
    return array_values($menusCurrentLang);
}

And call it

wpml_wp_get_nav_menus();

instead of

wp_get_nav_menus();

Hopefully, this will fix your issue.

Regards,

July 13, 2020 at 4:36 pm #6585923

paulM-12

Thanks! The code you provided works!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.