2015-08-25 128 views
2

我想自定义菜单添加到我的主菜单,我已经使用这个下面这段代码,如何自定义菜单添加到特定位置的WordPress

add_filter('wp_nav_menu_items', 'search_menu_item', 10, 2); 
function search_menu_item ($items, $args) { 
if ($args->theme_location == 'secondary-menu') { 
$items .= '<li class="border-none">SEARCH<form><input type="text" name="s" placeholder="Search Here" class="search-box"></form>'; 
} 
return $items; 
} 

和菜单显示为最后的菜单,但我想将我的菜单添加到第三个位置。我该怎么做

任何人都可以帮忙?

感谢

回答

6

你还是使用wp_nav_menu_objects过滤器,它可让你修改的项目,而不是一个字符串数组。

例子:

add_filter('wp_nav_menu_objects', 'restructure_menu_links', 10, 2); 

function restructure_menu_links($items, $args) { 

    $new_links = array(); 

    $label = 'Lorem Ipsum'; // add your custom menu item content here 

    // Create a nav_menu_item object 
    $item = array(
     'title'   => $label, 
     'menu_item_parent' => 0, 
     'ID'    => 'yourItemID', 
     'db_id'   => '', 
     'url'    => $link, 
     'classes'   => array('menu-item') 
    ); 

    $new_links[] = (object) $item; // Add the new menu item to our array 

    // insert item 
    $location = 3; // insert at 3rd place 
    array_splice($items, $location, 0, $new_links); 

    return $items; 
} 
+0

感谢您的回复, 现在我能到我的菜单添加到我的愿望的位置,但我怎么加我的搜索形式在里面。 – Mayank

+0

@Mayank'get_search_form'这个函数会得到你的表格 – Musk

+0

@Musk get_search_form将在菜单中添加搜索表单,但是我想要的是,搜索文本作为菜单项,当用户点击它时,搜索表单从右到左切换。 意味着我想要我的搜索菜单项li是这样的

  • 搜索
  • Mayank

    相关问题