Viewing 7 posts - 1 through 7 (of 7 total)
  • Posted in: Dante
  • #298694
    ronin
    Member
    Post count: 282

    Hi,

    On one of our websites we use multiple menus in order to distinguish between different services, and the client is wanting to have Spanish pages (WPML). By default WPML only works on one menu, and we forwarded the suggested work around on this page to our developer and he has said that it isn’t working, and was wondering if there was any reason why this wouldn’t work, or if there’s something he’s missing?

    His message:

    I’ve added the code snippet as provided here (https://wpml.org/forums/topic/language-switcher-on-the-menu/) to add WPML menu programatically to any menu location, however, it’s not working for some reason.

    I would suggest contacting the theme author and asking them if there’s any reason why that wouldn’t work in the child-theme functions.php file as expected.

    function wpml_add_menu_item( $items, $args ) {
    $theme_locations = array( ‘main_navigation’);
    if( !empty( $args->theme_location ) && in_array( $args->theme_location, $theme_locations ) ) {
    ob_start(); ?>
    <li class=”menu-item ls-menu”><?php echo do_action(‘icl_language_selector’); ?>
    <?php
    $items .= ob_get_contents();
    ob_end_clean();
    }
    return $items;
    }
    add_filter(‘wp_nav_menu_items’, ‘wpml_add_menu_item’, 10, 2);

    Thanks.

    #299119
    Rui Guerreiro – SUPPORT
    Keymaster
    Post count: 25779

    Hi,

    I have some doubts, it will be an entire spanish version of the site right?
    If so you don’t need any code, using wmpl it will let you translate the menu to your desired language.

    https://wpml.org/documentation/getting-started-guide/translating-menus/

    -Rui

    #299270
    ronin
    Member
    Post count: 282

    Hi Rui,

    The issue isn’t that we need to translate the menu – that’s fine – it’s that we have multiple types of menus for each of the services (if you look at the home page each of those services has their own unique menus). And we want to have the flag to help you change language on every menu, but unfortunately WPML by default only lets you have the flag/language changer on one menu. We found a workaround, but it hasn’t worked and were wondering if you would be able to point us in the right direction.

    This is quite urgent and any help would be greatly appreciated.

    #299691
    David Martin – Support
    Moderator
    Post count: 20834

    @ronin rather than trying to hook into the menu function, is there any reason you cannot simply add the shortcode next to the menu display via a child theme?

    Happy to help you do this, maybe I am misunderstanding what you want – it sounds like you want to language switcher next to all menu’s regardless of the current language menu.

    #299716
    ronin
    Member
    Post count: 282

    Hi David,

    We don’t particularly mind how we do it, a shortcode next to the menu display would work, if you would be able to help us do that.

    Ideally we would like the language switcher to show up on any page (regardless of its menu) that has a spanish translation (as we don’t have translations of every page). If this is possible, help to do that would be amazing!

    Failing that, just being able to add the language switcher into every menu (i.e. the menu for process innovation, metallurgical pilot plants etc) so that it’s on every page, would be the next best option.

    Thanks so much.

    #299719
    ronin
    Member
    Post count: 282

    Also, I just wanted to reiterate that our developer has already added code that is supposed to add the language switcher to all menus but he has said that it’s not working with the theme somehow. If you could have a look to see why that is, then we could just edit the existing code.

    But yes I just wanted to clarify that we want the language switcher to appear on every menu type (in essence so it shows up on every page, regardless of whether it’s the Analytical Services menu, Metallurgical Pilot Plants menu etc). So if you could provide us that code to go into the child theme (if you can’t figure out why the existing code isn’t working) then we can use CSS to hide the language changer except for on pages with a translation.

    Thanks

    #300062
    David Martin – Support
    Moderator
    Post count: 20834

    Hi,

    The function provided by WPML works, if you test this, you will see it injects a menu item to the end of the menu called “MENU ITEM” (my testing purposes only).

    function wpml_add_menu_item( $items, $args ) {
    	$theme_locations = array( 'main_navigation');
    		if( !empty( $args->theme_location ) && in_array( $args->theme_location, $theme_locations ) ) {
    			ob_start(); 
    			?>
    			<li class="menu-item-51 menu-item menu-item-type-post_type menu-item-object-page"><a>MENU ITEM</a></li>
    			<?php
    			$items .= ob_get_contents();
    			ob_end_clean();
    		}
    		return $items;
    }
    add_filter('wp_nav_menu_items', 'wpml_add_menu_item', 10, 2);

    Initially WPML provide you with a WordPress action, rather than my “Menu Item” test link. <?php echo do_action('icl_language_selector'); ?>

    What function are you or your web dev using to hook into that action? You will need to hook into that action to display a function in that position as per their docs: https://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.