给你。既然你已经有了你所需要的菜单idis:
$menu_item_defaults = array(
\'menu-item-db-id\' => $menu_item_db_id,
\'menu-item-object-id\' => 0,
\'menu-item-object\' => \'\',
\'menu-item-parent-id\' => 0,
\'menu-item-position\' => 1,
\'menu-item-title\' => \'Play Game\',
\'menu-item-url\' => \'\',//not sure is a blank is ok here
\'menu-item-description\' => \'\',
\'menu-item-attr-title\' => \'\',
\'menu-item-target\' => \'\',
\'menu-item-classes\' => \'\',
\'menu-item-xfn\' => \'\',
\'menu-item-status\' => \'publish\',
);
wp_update_nav_menu_item(\'primary-menu\', 0, $menu_item_defaults);
但是,如果您试图加快具有已知内容的站点的部署,请尝试这种方式。您可以动态创建页面并将其添加到菜单中。
在我的例子中,我首先创建一个自定义菜单,但您可以使用它来获取任何菜单的id。
$menu_exists = wp_get_nav_menu_object( \'Public Menu\' );
if ( $menu_exists === false ) {
$var_pub_menu_id = wp_create_nav_menu( \'Public Menu\' );
} else {
$var_pub_menu_id = $menu_exists->term_id;
}
然后我创建页面并将其添加到菜单:
$default_fgpsn_page = array(
\'post_title\' => $fgpsn_default_pages[$j],//any page title. I\'m working in a loop here
\'post_status\' => \'publish\',
\'post_type\' => \'page\',
\'post_content\' => \'Property Browse Short Code\',//this can even be a shortcode
\'post_author\' => 1
);
$default_fgpsn_page_id = wp_insert_post( $default_fgpsn_page );
$menu_item_defaults = array(
//\'menu-item-db-id\' => $menu_item_db_id,
//\'menu-item-object-id\' => 0,
//\'menu-item-object\' => \'\',
\'menu-item-parent-id\' => 0,
\'menu-item-position\' => 1,
\'menu-item-title\' => $fgpsn_default_pages[$j],
\'menu-item-url\' => get_page_link( $default_fgpsn_page_id ),
\'menu-item-description\' => \'public properties listing\',
\'menu-item-attr-title\' => \'\',
\'menu-item-target\' => \'\',
\'menu-item-classes\' => \'\',
\'menu-item-xfn\' => \'\',
\'menu-item-status\' => \'publish\',
);
wp_update_nav_menu_item($var_pub_menu_id, 0, $menu_item_defaults);