Create a default menu containing the first 15 top-level pages. see #11817
git-svn-id: http://svn.automattic.com/wordpress/trunk@13406 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
46ae8913d3
commit
b77baf5afb
|
@ -56,6 +56,12 @@ if ( isset( $_POST[ 'delete-menu' ] ) && $menu_selected_id > 0 ) {
|
||||||
|
|
||||||
// Default Menu to show
|
// Default Menu to show
|
||||||
$custom_menus = wp_get_nav_menus();
|
$custom_menus = wp_get_nav_menus();
|
||||||
|
|
||||||
|
if ( empty($custom_menus) && empty($_POST) ) {
|
||||||
|
wp_create_default_nav_menu();
|
||||||
|
$custom_menus = wp_get_nav_menus();
|
||||||
|
}
|
||||||
|
|
||||||
if ( ! $menu_selected_id && ! empty( $custom_menus ) )
|
if ( ! $menu_selected_id && ! empty( $custom_menus ) )
|
||||||
$menu_selected_id = $custom_menus[0]->term_id;
|
$menu_selected_id = $custom_menus[0]->term_id;
|
||||||
|
|
||||||
|
|
|
@ -21,19 +21,43 @@ function wp_delete_nav_menu( $menu_id ) {
|
||||||
wp_delete_term( $menu_id, 'nav_menu' );
|
wp_delete_term( $menu_id, 'nav_menu' );
|
||||||
}
|
}
|
||||||
|
|
||||||
function wp_create_nav_menu( $menu_name ) {
|
function wp_create_nav_menu( $menu_name, $args = array() ) {
|
||||||
$menu_exists = get_term_by( 'name', $menu_name, 'nav_menu' );
|
$menu_exists = get_term_by( 'name', $menu_name, 'nav_menu' );
|
||||||
|
|
||||||
if ( $menu_exists )
|
if ( $menu_exists )
|
||||||
return new WP_Error('menu_exists', sprintf( __('A menu named “%s” already exists; please try another name.'), esc_html( $menu_exists->name ) ) );
|
return new WP_Error('menu_exists', sprintf( __('A menu named “%s” already exists; please try another name.'), esc_html( $menu_exists->name ) ) );
|
||||||
|
|
||||||
$menu = wp_insert_term( $menu_name, 'nav_menu' );
|
if ( isset($args['slug']) )
|
||||||
|
$slug = $args['slug'];
|
||||||
|
else
|
||||||
|
$slug = $menu_name;
|
||||||
|
|
||||||
|
$menu = wp_insert_term( $menu_name, 'nav_menu', array('slug' => $slug) );
|
||||||
|
|
||||||
if ( is_wp_error($menu) )
|
if ( is_wp_error($menu) )
|
||||||
return $menu;
|
return $menu;
|
||||||
|
|
||||||
return get_term( $menu['term_id'], 'nav_menu');
|
return get_term( $menu['term_id'], 'nav_menu');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function wp_create_default_nav_menu() {
|
||||||
|
$menu = wp_create_nav_menu(__('Main'), array('slug' => 'main'));
|
||||||
|
|
||||||
|
if ( is_wp_error($menu) )
|
||||||
|
return;
|
||||||
|
|
||||||
|
$pages = get_pages( array('parent' => 0, 'number' => 15) );
|
||||||
|
$counter = 1;
|
||||||
|
foreach ( $pages as $page ) {
|
||||||
|
$item = array('post_status' => 'publish', 'post_type' => 'nav_menu_item', 'menu_order' => $counter, 'tax_input' => array( 'nav_menu' => $menu->name), 'post_title' => addslashes($page->post_title) );
|
||||||
|
$item_id = wp_insert_post($item, true);
|
||||||
|
update_post_meta( $item_id, 'menu_type', 'page' );
|
||||||
|
update_post_meta( $item_id, 'object_id', $page->ID );
|
||||||
|
update_post_meta( $item_id, 'menu_new_window', 0 );
|
||||||
|
$counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function wp_get_nav_menu( $menu ) {
|
function wp_get_nav_menu( $menu ) {
|
||||||
return get_term( (int) $menu, 'nav_menu');
|
return get_term( (int) $menu, 'nav_menu');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue