2017-10-12 46 views
0

我在菜单中显示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); 

回答

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; 

我想这应该工作! :)

+0

后矿井答案似乎做同样的事情却多了几分复杂,环路unneassery我的想法! 我想上发表评论,但不幸的是我可以在我的文章发表评论,所以如果你考验我的解决方案,它的作品请投我... :) – ToTaTaRi

1

我已经找到一种方法来做到这一点:我分裂阵列中的菜单项,以便能够插入你的语言切换器在第2位。

我已经重新审视你的代码位:

// 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+

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); 

我认为这是你们提出的同样的方法。谢谢。