我需要在飞行中向导航菜单添加自定义链接。我可以将自定义链接添加到项目的第一级(通过外观>菜单创建),但由于某些原因,我无法添加自定义链接,即之前创建的另一个自定义链接的子级。。
这是我的代码:
function on_the_fly($items) {
$menu_items = array();
foreach ($items as $item) {
$item->menu_order = count($menu_items) + 1;
$menu_items[] = $item;
$new_menu_item = new \\WP_Post((object) array(
\'ID\' => $item->ID . "00",
));
$new_menu_item->post_author = 1;
$new_menu_item->comment_status = \'closed\';
$new_menu_item->ping_status = \'closed\';
$new_menu_item->post_type = \'nav_menu_item\';
$new_menu_item->post_title = \'test 1\';
$new_menu_item->post_name = \'test 1\';
$new_menu_item->filter = \'raw\';
$new_menu_item->object_id = $new_menu_item->ID;
$new_menu_item->object = \'custom\';
$new_menu_item->type = \'custom\';
$new_menu_item->type_label = \'Custom Link\';
$new_menu_item->menu_order = count($menu_items) + 1;
$new_menu_item->menu_item_parent = $item->ID;
$new_menu_item->url = str_replace("//", "/", $item->url . "/" . sanitize_title(\'test 1\'));
$new_menu_item->title = \'test 1\';
$menu_items[] = $new_menu_item;
$new_menu_item_2 = new \\WP_Post((object) array(
\'ID\' => $new_menu_item->ID . "00",
));
$new_menu_item_2->post_author = 1;
$new_menu_item_2->comment_status = \'closed\';
$new_menu_item_2->ping_status = \'closed\';
$new_menu_item_2->post_type = \'nav_menu_item\';
$new_menu_item_2->post_title = \'test 2\';
$new_menu_item_2->post_name = \'test 2\';
$new_menu_item_2->filter = \'raw\';
$new_menu_item_2->object_id = $new_menu_item->ID;
$new_menu_item_2->object = \'custom\';
$new_menu_item_2->type = \'custom\';
$new_menu_item_2->type_label = \'Custom Link\';
$new_menu_item_2->menu_order = count($menu_items) + 1;
$new_menu_item_2->menu_item_parent = $new_menu_item->ID;
$new_menu_item_2->url = str_replace("//", "/", $item->url . "/" . sanitize_title(\'test 2\'));
$new_menu_item_2->title = \'test 2\';
$menu_items[] = $new_menu_item_2;
}
return $menu_items;
}
add_filter(\'wp_nav_menu_objects\', \'on_the_fly\', 1,1);
结果:
> - Application
> - test 1
> - test 2
而不是:
> - Application
> - test 1
> - test 2
我怎样才能达到预期的结果?