所以我将一些自定义菜单项编码为动态生成的下拉菜单。它工作正常,但后来我意识到它们也被保存到数据库中。我想创建挂钩到主菜单但不保存在数据库中的下拉菜单,因为它们对于每个用户都不同,具体取决于用户元素,任何想法?
这也是一个儿童主题,所以必须挂钩到主菜单,没有选择生成新菜单等作为一个可怕的主题建设者的一部分。
function _custom_nav_menu_item( $title, $url, $order, $parent = 0 ){
$item = new stdClass();
$item->ID = 1000000 + $order + parent;
$item->db_id = $item->ID;
$item->title = $title;
$item->url = $url;
$item->menu_order = $order;
$item->menu_item_parent = $parent;
$item->type = '';
$item->object = '';
$item->object_id = '';
$item->classes = array();
$item->target = '';
$item->attr_title = '';
$item->description = '';
$item->xfn = '';
$item->status = '';
return $item;
}
// Custom nav menu. Adding dropdowns etc according to user permissions.
function custom_nav_menu_items( $items, $menu ){
$user_permissions = user_is_loggedin_with_permissions();
if( $menu->slug == 'main-menu-english' ){
// Announcements dropdown, users allowed regions.
$top = _custom_nav_menu_item( 'Announcements', site_url( '/announcements' ), 2 );
$items[] = $top;
$i = 1;
foreach( $user_permissions['Region'] as $region ){
$for_humans = ucfirst( str_replace( '_', ' ', $region ) );
$items[] = _custom_nav_menu_item( $for_humans, site_url( '?post_type=announcements®ions='.$region ), '10'.$i, $top->ID );
$i++;
}
}
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'custom_nav_menu_items', 20, 2 );