Replies: 0
I’ve been using Polylang for over 5 years now and had some clients with websites in 4 different languages with menus with ~50 items. You could imagine how tedious it has been to sync those menus or update them. Well here’s my initial solution, which as I see it, is a necessity missing from Polylang.
function jeesus_wp_get_nav_menu_items($items) {
$new_items = [];
foreach ($items as $item) {
$translatedID = pll_get_post($item->object_id);
if (!$translatedID) continue;
$translated = get_post($translatedID);
$item->post_title = $translated->post_title;
$item->guid = $translated->guid;
$item->object_id = $translated->ID;
$item->url = get_permalink($translated->ID);
$item->title = $translated->post_title;
$new_items[] = $item;
}
return $new_items;
}
if (!is_admin()) add_filter( 'wp_get_nav_menu_items', 'jeesus_wp_get_nav_menu_items', 20 );
Sure, this example assumes that
- you want your multilingual menus to be identical (but with little more code they don’t have to)
- all your menu items are actual posts
But you get the idea how to go from here. 🙂