如何在Wordpress中将自定义菜单添加到特定位置

如何在Wordpress中将自定义菜单添加到特定位置,wordpress,menuitem,Wordpress,Menuitem,我想在我的主菜单中添加自定义菜单,我使用了下面的代码 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><

我想在我的主菜单中添加自定义菜单,我使用了下面的代码

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;
}
add_filter('wp_导航菜单项','search_菜单项',10,2);
函数搜索\菜单\项($items,$args){
如果($args->theme\u location=='secondary menu'){
$items.='
  • 搜索'; } 退回$items; }
  • 菜单显示为最后一个菜单,但我想将菜单添加到第三个位置。我该怎么做

    有人能帮忙吗


    谢谢

    您应该改用wp\u nav\u菜单\u 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;
    }
    

    以下是在WordPress中添加自定义菜单的步骤

  • 首先,进入你的管理->外观->菜单
  • 通过单击“+”符号创建一个新菜单,如图所示
  • 添加新菜单时,您可以在左侧“主导航”中看到添加的菜单。现在,您可以从“自定义链接”、“页面”和“类别”添加菜单项
  • 添加菜单项时,可以在右侧看到该项。你可以在上图中看到“雅虎”。它是一个外部链接菜单项。如果要使任何菜单项成为任何其他菜单项的子页面,只需拖动该菜单链接并放置在父菜单项下方。见上图。在上图中,“未分类”是“雅虎”的子代
  • 您可以创建任意数量的菜单。但您可以显示与主题兼容的菜单。您可以创建任意数量的菜单
  • 只要放置这一行代码,您就可以在任何地方调用菜单

           <?php $defaults = array( 'theme_location'  => ,
           'menu'            => ,
           'container'       => 'div',
           'container_class' => 'menu-{menu slug}-container',
           'container_id'    => ,
           'menu_class'      => 'menu',
           'menu_id'         => ,
           'echo'            => true,
           'fallback_cb'     => 'wp_page_menu',
           'before'          => ,
           'after'           => ,
           'link_before'     => ,
           'link_after'      => ,
           'items_wrap'      => '
           <ul id="%1$s" class="%2$s">%3$s</ul>',
           'depth'           => 0,
           'walker'          => );
           ?>
           <?php wp_nav_menu( $args ); ?> 
    

    谢谢回复,现在我可以将我的菜单添加到我想要的位置,但如何在其中添加搜索表单。@Mayank
    获取搜索表单
    此功能将为您获取form@Musk获取搜索表单将在菜单中添加搜索表单,但我想,搜索文本作为菜单项,当用户点击它时,搜索表单从右向左切换。意思是我希望我的搜索菜单项li是这样的
  • 搜索
  • 多年后,这个答案帮助了我。我知道这是旧的,有机会添加子菜单项,所以我可以查询一些帖子,它们会自动更新。