• Hello and thank you for the awesome plug-in.
    I have a menu location set up and I need to gather menu items for each existing language to serve them up as JSON.

    I am using:
    $term = wp_nav_menu(array(‘theme_location’ => ‘my-location’));
    $menu_id = $term->term_id;
    $menu_items = wp_get_nav_menu_items($menu_id);

    This way I only get the menu in the main language.
    How do I pass a language parameter over to wp_nav_menu ?

    Thanks!
    M.

    https://www.ads-software.com/plugins/polylang/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Mauro

    (@maurodalu)

    Here is my current code…

    // Create an array with all the languages, cleanded up
    	$languages = pll_the_languages(array('raw'=>1));
    	$menus = array();
    	foreach($languages as $language) {
    		//$current_language = $language['slug'];
    		$theme_location = 'mobile-app-menu';
    		$locations = get_nav_menu_locations();
    		$menu_id = $locations[$theme_location];
    		$menu_items = wp_get_nav_menu_items($menu_id);
    		$menu = array();
    		foreach ($menu_items as $menu_item) {
    		    $trimmed_menu_item['object_id'] = $menu_item->object_id;
    		    $trimmed_menu_item['menu_item_parent'] = $menu_item->menu_item_parent;
    		    $trimmed_menu_item['menu_order'] = $menu_item->menu_order;
    		    $trimmed_menu_item['title'] = $menu_item->title;
    		    $trimmed_menu_item['description'] = $menu_item->description;
    		    $trimmed_menu_item['post_status'] = $menu_item->post_status;
    		    $trimmed_menu_item['object'] = $menu_item->object;
    		    $trimmed_menu_item['url'] = $menu_item->url;
    		    array_push($menu, $trimmed_menu_item);
    		}
    		array_push($menus, $menu);
    	}
    	return $menus;

    Plugin Author Chouby

    (@chouby)

    Hi!

    The correct nav_menu is stored in the ‘polylang’ option. You can get it with these keys:
    ['nav_menus'][$theme][$nav_menu_location][$language_slug]

    Hi based on Chouby’s answer, here’s a simple function to get the menu ID based on the menu’s theme_location:

    function get_language_menu_id( $theme_location = '' ) {
    	// Check for theme location
    	if ( $theme_location == '' )
    		return 0;
    
    	// Get Polylang options
    	$options = get_option( 'polylang' );
    	if ( $options && isset( $options['nav_menus']['your_theme_name'][ $theme_location ] ) ) {
    		$current_language = pll_current_language();
    		if ( isset( $options['nav_menus']['your_theme_name'][ $theme_location ][ $current_language ] ) )
    			return $options['nav_menus']['your_theme_name'][ $theme_location ][ $current_language ];
    	}
    
    	// Something went wrong
    	return 0;
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to pass a language parameter to wp_nav_menu ?’ is closed to new replies.