• Resolved lksz

    (@lksz)


    Ok so I am trying to create a custom dropdown button for language selection, I used the filter t add a button, but for some reason a list of languages appears in 2 places(inside a dropdawn button, which is what i want and and at the beggining of the navbar). Not sure what did i do wrong here. Adding a code snippet for filter which I’m using and a screenshot link, to represent my problem better

    add_filter( 'wp_nav_menu_items', 'custom_menu_item', 10, 2 );
    function custom_menu_item ( $items, $args ) {
    
        if (is_page() && $args->theme_location == 'main-nav') {
            $items .= '<li class="dropdown">';
            $items .= '<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">';
                    if (function_exists('pll_current_language')){
                    $items .= pll_current_language('name');
                    };
            $items .= '<span class="caret"></span>';
            $items .= '</button>';
            $items .= '<ul class="lang-switch dropdown-menu" aria-labelledby="dropdownMenu1">';
                    if (function_exists('pll_the_languages')){
                    $items .= pll_the_languages(array('show_flags'=>1,'show_names'=>1));
                    };
            $items .= '</ul>';
            $items .= '</li>';
        }
        return $items;
    }

    https://wordpress.org/plugins/polylang/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter lksz

    (@lksz)

    Screenshot link:

    <img src=’http://s8.postimg.org/aj5prxoy9/menu_bug.jpg&#8217; border=’0′ alt=”menu bug” />

    Plugin Author Chouby

    (@chouby)

    Hi!

    Here is the fix:

    $items .= pll_the_languages(array('echo' => 0, 'show_flags'=>1,'show_names'=>1));

    Thread Starter lksz

    (@lksz)

    Thanks for reply mate, I actually just managed to figure it out using this aproach:

    add_filter( 'wp_nav_menu_items', 'custom_menu_item', 10, 2 );
            function custom_menu_item ( $items, $args ) {
                if (is_page() && $args->theme_location == 'main-nav') {
                    $languages = pll_the_languages(array('raw' => 1));
    
                    $items .= '<li class="dropdown">';
                    $items .= '<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">';
                        if (function_exists('pll_current_language')){
                            $items .= pll_current_language('name');
                        };
                    $items .= '<span class="caret"></span>';
                    $items .= '</button>';
                    $items .= '<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">';
                        foreach ($languages as $lang) {
                            $items.= '<div class = lang-item-"' . $lang['slug'] . '">';
                            $items.=  '<a href ="' . $lang['url'] . '" hreflang = "' . $lang['slug'] . '">' . $lang['name'] . '</a>';
                            $items.=  '</div>';
                        }
                    $items .= '</ul></li>';
    
                }
                return $items;
            }

    Although one more thing is left to take care of. I would like to have a flag icon next to the selected language on my dropdawn button. Any ideas how it could be easily implemented ?

    Plugin Author Chouby

    (@chouby)

    To include the flag in the dropdown, you need to use a javascript library. There are select2, jqueryui.selectmenu and some other which enable this feature.

    Thread Starter lksz

    (@lksz)

    OK thanks, I’ll mark this topic as resolved.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Custom dropdawn duplication using pll_the_languages function.’ is closed to new replies.