我用下面的代码自动创建页面,然后我创建一个自定义菜单并将所有页面插入该菜单。
$create_pages = array(\'Home\', \'About\', \'Contact\'); // there will be more items
$count = 1;
foreach ($create_pages as $new_page) {
$add_pages = array(
\'post_title\' => $new_page,
\'post_content\' => \'\',
\'post_status\' => \'publish\',
\'post_type\' => \'page\',
\'menu_order\' => $count++ // incrementing the order value to overwrite default alphabetical ordering
);
$page_id = wp_insert_post($add_pages);
}
到目前为止,我成功地覆盖了默认的字母顺序,现在在页面屏幕上,所有页面都按我想要的方式排序。
$main_nav_id = wp_create_nav_menu(\'Main menu\', array(\'slug\' => \'main-nav\'));
set_theme_mod(\'nav_menu_locations\', array(\'main-nav\' => $main_nav_id));
$pages = get_pages();
$count = 1;
foreach($pages as $page) {
$item = array(
\'menu-item-object-id\' => $page->ID,
\'menu-item-object\' => \'page\',
\'menu-item-type\' => \'post_type\',
\'menu-item-status\' => \'publish\',
\'menu-item-position\' => $count++ // im trying to use the same approach
);
wp_update_nav_menu_item($main_nav_id, 0, $item);
}
因此,对于这段代码,我尝试使用与之前相同的方法来处理页面,但无论我尝试什么,菜单项仍然以一种奇怪的方式排序,我只是希望它们的排序方式与页面屏幕上的排序方式相同。我怎样才能解决这个问题?