将自定义菜单移动到特定位置

时间:2017-10-12 10:50:07

标签: wordpress woocommerce wpml storefront

我在菜单中显示了WPML语言切换器,在底部显示了最后一项。我希望它在二级菜单的第二个位置。

如何更改我的代码以实现这一目标?

当前代码:

/*Display WPML language switcher in the menu */
function wpml144107($menu, $args){

    if ( ! is_admin() ):
        if( $args->theme_location == 'secondary' ):
            if(function_exists('icl_get_languages')):
                $languages = icl_get_languages('skip_missing=0&orderby=custom');

                if(count($languages) >= 1):

                    $flags = '<li id="menu-item-lang"><div class="flags_top">';
                    //foreach((array)$languages as $language):
                            //echo print_r($languages);
                            $language = $languages['fr'];
                            $flags .= '
                            <span class="icl-'. $language['language_code'] . ( $language['active'] == 1 ? ' icl-current' : '' )  .'">          
                                <a rel="alternate" hreflang="' . $language['language_code'] . '" href="' . $language['url']. '">' . $language['language_code'] . '</a>
                            </span>  /';

                            $language = $languages['en'];
                            $flags .= '
                            <span class="icl-'. $language['language_code'] . ( $language['active'] == 1 ? ' icl-current' : '' )  .'">          
                                <a rel="alternate" hreflang="' . $language['language_code'] . '" href="' . $language['url']. '">' . $language['language_code'] . '</a>
                            </span>';

                    //endforeach;
                    $flags .= '</div></li>';
                endif;

                return $menu . $flags;
            endif;
        endif;
    endif;
    return $menu;
}
add_action('wp_nav_menu_items', 'wpml144107', 50, 2);

3 个答案:

答案 0 :(得分:1)

此时:

return $menu . $flags;

语言菜单会附加到菜单中。

如果您将其更改为:

return $flags . $menu;

它将处于第一位......

但是你想在第二个拥有它,对吗?然后它有点复杂,你必须拆分包含菜单的字符串,我想每个元素都在<li>元素内:

/* finds the position of the first occurrence of </li>, 
so the end of the first element, add 4 because we want the position of 
the end of the </li>-tag not the start... */
$splitpos = strpos($menu, '</li>') + 4; 

// cut the string up to that position and you get the first element...
$firstelem = substr($menu, 0, $splitpos));

// cut after that position and you get the rest of the menu...
$rest = substr($menu, $splitpos));

// now return the menu with your language menu in between...
return $firstelem . $flags . $rest;

我想这应该有效! :)

答案 1 :(得分:1)

我找到了一种方法:我将菜单项拆分成阵列,以便能够将语言切换器插入第二位置。

我稍微重新审视了你的代码:

// Display WPML language switcher in 2nd position of the menu
function wpml144107($items, $args){

    if ( ! is_admin() && $args->theme_location == 'secondary' && function_exists('icl_get_languages') ):

        $languages = icl_get_languages('skip_missing=0&orderby=custom');

        if( count($languages) == 0 ) return $items; //  If there is no languages we return defaults menu items

        $html = 'id="menu-item-lang">
        <div class="flags_top">
            <span class="icl-'. $languages['fr']['language_code'] . ( $languages['fr']['active'] == 1 ? ' icl-current' : '' )  .'">
                <a rel="alternate" hreflang="' . $languages['fr']['language_code'] . '" href="' . $languages['fr']['url']. '">' . $languages['fr']['language_code'] . '</a>
            </span>  /';

        $html .= '
            <span class="icl-'. $languages['en']['language_code'] . ( $languages['en']['active'] == 1 ? ' icl-current' : '' )  .'">
                <a rel="alternate" hreflang="' . $languages['en']['language_code'] . '" href="' . $languages['en']['url']. '">' . $languages['en']['language_code'] . '</a>
            </span>';

        $html .= '</div>
        ';
        // Html in between each items (to split the items in an array)
        $glue = '</li>
<li ';
        // Spliting menu items in an array
        $items_array = explode( $glue, $items );

        // Counter
        $count = 0;
        foreach($items_array as $key => $item){
            // We insert in 2nd position the language switcher
            if($key == 1)
                $ordered_items[$count] = $html;
            else
                $ordered_items[$count] = $item;
            $count++;
        }
        // We set back the array of menu items in a correct html string
        $items = implode( $glue, $ordered_items );

    endif;

    return $items;
}
add_action('wp_nav_menu_items', 'wpml144107', 50, 2);

代码进入活动子主题(活动主题或任何插件文件)的function.php文件中。

在WooCommerce 3 +中测试并运行

答案 2 :(得分:0)

我使用this answer

解决了这个问题

使用此代码:

/*Display WPML language switcher in the secondary menu at the second position*/
function add_custom_in_menu( $items, $args ) 
{
    if( $args->theme_location == 'secondary' ):
        if(function_exists('icl_get_languages')):
            $languages = icl_get_languages('skip_missing=0&orderby=custom');
            if(count($languages) >= 1):

                $items_array = array();
                while ( false !== ( $item_pos = strpos ( $items, '<li', 3 ) ) )
                {
                    $items_array[] = substr($items, 0, $item_pos);
                    $items = substr($items, $item_pos);
                }
                $items_array[] = $items;

                $customHtml = '<li>bla</li>';
                $flags = '<li id="menu-item-lang"><div class="flags_top">';
                    //foreach((array)$languages as $language):
                        //echo print_r($languages);
                        $language = $languages['fr'];
                        $flags .= '
                        <span class="icl-'. $language['language_code'] . ( $language['active'] == 1 ? ' icl-current' : '' )  .'">          
                            <a rel="alternate" hreflang="' . $language['language_code'] . '" href="' . $language['url']. '">' . $language['language_code'] . '</a>
                        </span>  /';

                        $language = $languages['en'];
                        $flags .= '
                        <span class="icl-'. $language['language_code'] . ( $language['active'] == 1 ? ' icl-current' : '' )  .'">          
                            <a rel="alternate" hreflang="' . $language['language_code'] . '" href="' . $language['url']. '">' . $language['language_code'] . '</a>
                        </span>';

                //endforeach;
                $flags .= '</div></li>';

                // insert custom item after 2nd one
                array_splice($items_array, 1, 0, $flags); 

                $items = implode('', $items_array);

            endif;
        endif;
    endif;
    return $items;
}
add_filter('wp_nav_menu_items','add_custom_in_menu', 10, 2);

我认为这与你们建议的方法相同。谢谢。