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

3

我想将自定义菜单添加到我的主菜单中,我已经使用了以下代码:

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

我的菜单最后一个显示,但我想把它添加到第三个位置。我该怎么做?

有人能帮忙吗?

谢谢

2个回答

10
你应该使用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;
}

谢谢回复,现在我能够将我的菜单添加到我想要的位置,但是如何在其中添加搜索表单呢? - Mayank
@Mayank get_search_form 这个函数将会获取搜索表单。 - Musk
@Musk get_search_form会在菜单中添加搜索表单,但我想要将搜索文本作为菜单项,并且当用户单击它时,搜索表单从右侧切换到左侧。这意味着我希望我的搜索菜单项li像这样:<li class="border-none">搜索<form><input type="text" name="s" placeholder="在此处搜索" class="search-box"></form></li> - Mayank
这个答案帮助了我,多年以后。 - Stender
我知道这已经有些老了,但是否有可能添加子菜单项,以便我可以查询一些自动更新的帖子。 - Rodrigo Zuluaga

1
以下是在WordPress中添加自定义菜单的步骤。
  1. First of all, go to your admin-> appearance -> menus.
  2. Create a new menu using by clicking on the “+” sign as shown in the image.
  3. When you add a new menu, you can see the added menu in left “Primary Navigation”. Now you can add menu items from “custom links”, “Pages” and “Categories”.
  4. When you add menu items, you can see that item in the right side. You can see “Yahoo” in above image. Its an external link menu item. If you want to make any menu item as a child page of any other menu item, just drag that menu link and place below the parent. See above image. in Above image, “Uncategorized” is a child of “yahoo”.
  5. You can create as many menus as you want. But you can show menus as compatible with your theme. You can create as many menus as you want.
  6. You can call your menu wherever you want by just placing this one line code.

           <?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 ); ?> 
    
你可以根据需要选择参数。不要忘记在“menu”参数中包含菜单的名称。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接