Site logo

Archived topic

how to add WPML language switcher to Nav

7 replies · Started by Jochen on April 13, 2019

Viewing posts 1–8 of 8

Hi,

I am working on a multilanguage page using WPML and Elementor. Although I can have WPML add the switcher as first or last Menu-Element this is not the look that I want and does not work if set to DropDown.

I think it might be a good idea to add a widget area to the Nav area and add the swithcer there with some optical adjustments. Is there a howto to help with this?

Thx
Jochen

Hi there,

You could register a new widget area:

add_action( 'widgets_init', function() {
    register_sidebar( array(
        'name'          => 'Your Widget Area Name',
        'id'            => 'your-widget-area-id',
        'before_widget' => '<div>',
        'after_widget'  => '</div>',
        'before_title'  => '<h2>',
        'after_title'   => '</h2>',
    ) );
} );

Then you can:

1. Create a new Hook Element: https://docs.generatepress.com/article/hooks-element-overview/
2. Set the hook to inside_navigation
3. Add the widget area to the content:

<div class="wpml-switcher">
    <?php dynamic_sidebar( 'your-widget-area-id' ); ?>
</div>

4. Check the Execute PHP checkbox
5. Set your Display Rules

Now you can add some CSS like this:

.wpml-switcher {
    float: right;
}

Hello, I have two questions: is this still the code I can use for this task and second: how do I get or set the "your-widget-area-id"? Thank you.

Hi there,

yes you can use this code for adding the WPML widget to the navigation.

In the first block of code, you set your own name and area-id:

'name'          => 'Your Widget Area Name',
'id'            => 'your-widget-area-id',

Hello again, it seems that I can have anything as "ID" here, right? My problem is, that it does not want to float right (but it stays in the navigation left), as I have "navigation as a header" floating right also.

That is correct you can put anything that in the ID as long as it is not being used by another widget area :)
I would do this for example:

db-navigation-switcher-widget

the db is my initials so will reduce the chance that the ID has already been used.

If you want to raise a new topic where you can privately share a link to your site i'll take a look

Thank you - i have replied.

This archived topic is closed to new replies.