Last updated on October 18, 2023

WPML translate navigation menu

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Translate navigation menus using WPML.

To translate a navigation menu using WPML (WordPress Multilingual Plugin), you can use the wp_nav_menu_items filter hook along with the icl_get_languages function provided by WPML. This allows you to modify the navigation menu items based on the current language.

Here’s an example code snippet that demonstrates how to translate a navigation menu using WPML:

function wpsnippets_translate_menu_items($items, $args) {
    // Check if WPML is active
    if (function_exists('icl_get_languages')) {
        // Get the current language
        $current_language = ICL_LANGUAGE_CODE;

        // Get all available languages
        $languages = icl_get_languages('skip_missing=0');

        // Loop through each menu item
        foreach ($items as $item) {
            // Get the translated version of the menu item
            $translated_item = apply_filters('wpml_object_id', $item->object_id, 'nav_menu_item', TRUE, $languages[$current_language]['language_code']);

            // If a translated version exists, replace the original menu item with the translated one
            if ($translated_item != $item->object_id) {
                $item->object_id = $translated_item;
            }
        }
    }

    return $items;
}
add_filter('wp_nav_menu_objects', 'wpsnippets_translate_menu_items', 10, 2);

This code snippet hooks into the wp_nav_menu_objects filter and checks if WPML is active. It then retrieves the current language and all available languages using the icl_get_languages function. Next, it loops through each menu item and uses the wpml_object_id filter to get the translated version of the menu item. If a translated version exists, it replaces the original menu item with the translated one.

This code snippet can be useful when you have a multilingual website and want to display a translated version of the navigation menu based on the current language. It ensures that the correct menu items are displayed to users based on their language preference.

Examples

Example 1: Translate Navigation Menu using WPML

This example demonstrates how to translate a navigation menu using WPML. The code snippet retrieves the menu items for a specific menu location and displays them in the current language.

function wpsnippets_translate_navigation_menu() {
    if ( function_exists( 'wp_nav_menu' ) ) {
        $menu_location = 'primary'; // Replace with your menu location slug
        $menu_items = wp_get_nav_menu_items( $menu_location );

        if ( $menu_items ) {
            foreach ( $menu_items as $menu_item ) {
                $translated_menu_item = apply_filters( 'wpml_object_id', $menu_item->ID, 'nav_menu_item' );
                echo '<li><a href="' . esc_url( get_permalink( $translated_menu_item ) ) . '">' . esc_html( $menu_item->title ) . '</a></li>';
            }
        }
    }
}

This code retrieves the menu items for a specific menu location using wp_get_nav_menu_items(). It then loops through each menu item and uses the wpml_object_id filter to get the translated menu item ID. Finally, it displays the translated menu item title and URL.

Example 2: Translate Navigation Menu with Custom Walker

This example demonstrates how to translate a navigation menu using a custom walker class. The code snippet extends the Walker_Nav_Menu class and overrides the start_el() method to display the translated menu item.

class WPSnippets_Translated_Menu_Walker extends Walker_Nav_Menu {
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $translated_menu_item = apply_filters( 'wpml_object_id', $item->ID, 'nav_menu_item' );
        $output .= '<li><a href="' . esc_url( get_permalink( $translated_menu_item ) ) . '">' . esc_html( $item->title ) . '</a></li>';
    }
}

// Usage: wp_nav_menu( array( 'walker' => new WPSnippets_Translated_Menu_Walker() ) );

This code defines a custom walker class WPSnippets_Translated_Menu_Walker that extends the Walker_Nav_Menu class. It overrides the start_el() method to retrieve the translated menu item ID using wpml_object_id filter and display the translated menu item.

Example 3: Translate Navigation Menu with Polylang

This example demonstrates how to translate a navigation menu using Polylang plugin. The code snippet retrieves the menu items for a specific menu location and displays them in the current language.

function wpsnippets_translate_navigation_menu() {
    if ( function_exists( 'pll_current_language' ) ) {
        $menu_location = 'primary'; // Replace with your menu location slug
        $menu_items = wp_get_nav_menu_items( $menu_location );

        if ( $menu_items ) {
            foreach ( $menu_items as $menu_item ) {
                $translated_menu_item = pll_get_post( $menu_item->ID );
                echo '<li><a href="' . esc_url( get_permalink( $translated_menu_item ) ) . '">' . esc_html( $menu_item->title ) . '</a></li>';
            }
        }
    }
}

This code retrieves the menu items for a specific menu location using wp_get_nav_menu_items(). It then loops through each menu item and uses pll_get_post() function to get the translated menu item ID. Finally, it displays the translated menu item title and URL.

Last updated on October 18, 2023. Originally posted on November 17, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *