如何在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是这样的搜索 多年后,这个答案帮助了我。我知道这是旧的,有机会添加子菜单项,所以我可以查询一些帖子,它们会自动更新。