This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 2 replies, has 2 voices.

Last updated by adrienT-5 3 weeks, 5 days ago.

Assigned support staff: Noman.

Author Posts
September 19, 2019 at 2:35 pm #4606399

adrienT-5

Hi!

I am trying to: add a custom language switcher in my topbar.

So, I try to use the code indicated here at the bottom of the page https://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/

When I paste it to my function.php file in my child theme (Zoa theme), I have a php error.
When I paste the code in a php code tester, I see an error message "Unexpected "<" at line 28.

This is my complete function.php file

<?php 
// Filter wp_nav_menu() to add additional links and other output
// Show only other language in language switcher
// Use the new filter: https://wpml.org/wpml-hook/wpml_active_languages/
add_filter('wp_nav_menu_items', 'new_nav_menu_items', 10, 2);
function new_nav_menu_items($items, $args) {
    // uncomment this to find your theme's menu location
    //echo "args:<pre>"; print_r($args); echo "</pre>";

    // get languages
    $languages = apply_filters( 'wpml_active_languages', NULL, 'skip_missing=0' );

    // add $args->theme_location == 'primary-menu' in the conditional if we want to specify the menu location.

    if ( $languages && $args->theme_location == 'primary') {

        if(!empty($languages)){

            foreach($languages as $l){
                if(!$l['active']){
                    // flag with native name
                    $items = $items . '
<li class="menu-item"><a href="' . $l['url'] . '"><img src="' . $l['country_flag_url'] . '" height="12" alt="' . $l['language_code'] . '" width="18" /> ' . $l['native_name'] . '</a></li>

';
                    //only flag
                    //$items = $items . '
<li class="menu-item menu-item-language"><a href="' . $l['url'] . '"><img src="' . $l['country_flag_url'] . '" height="12" alt="' . $l['language_code'] . '" width="18" /></a></li>

';
                }
            }
        }
    }

    return $items;
}
 ?>

Thanks for your help.

September 19, 2019 at 3:31 pm #4606899

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting WPML Support. There was an issue due to single line php comment on line number 27. I have used multi line php comment and I hope now it will work fine. Here is updated code:

<?php 
// Filter wp_nav_menu() to add additional links and other output
// Show only other language in language switcher
// Use the new filter: https://wpml.org/wpml-hook/wpml_active_languages/
add_filter('wp_nav_menu_items', 'new_nav_menu_items', 10, 2);
function new_nav_menu_items($items, $args) {
    // uncomment this to find your theme's menu location
    //echo "args:<pre>"; print_r($args); echo "</pre>";
 
    // get languages
    $languages = apply_filters( 'wpml_active_languages', NULL, 'skip_missing=0' );
 
    // add $args->theme_location == 'primary-menu' in the conditional if we want to specify the menu location.
 
    if ( $languages && $args->theme_location == 'primary') {
 
        if(!empty($languages)){
 
            foreach($languages as $l){
                if(!$l['active']){
                    // flag with native name
                    $items = $items . '
<li class="menu-item"><a href="' . $l['url'] . '"><img src="' . $l['country_flag_url'] . '" height="12" alt="' . $l['language_code'] . '" width="18" /> ' . $l['native_name'] . '</a></li>
 
';
                    //only flag
                   /* $items = $items . '
<li class="menu-item menu-item-language"><a href="' . $l['url'] . '"><img src="' . $l['country_flag_url'] . '" height="12" alt="' . $l['language_code'] . '" width="18" /></a></li>';*/
                }
            }
        }
    }
 
    return $items;
}
 ?>

Please let me know if this resolves your issue or you need further assistance with this issue.

Thank you

September 19, 2019 at 3:57 pm #4607047

adrienT-5

My issue is resolved now. Thank you!