May*_*ank 2 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><input type="text" name="s" placeholder="Search Here" class="search-box"></form>';
}
return $items;
}
Run Code Online (Sandbox Code Playgroud)
菜单显示为最后一个菜单,但我想将菜单添加到第3个位置.我该怎么做呢
有人可以帮忙吗?
谢谢
您应该使用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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2941 次 |
| 最近记录: |