Make WordPress Core

Changeset 13406


Ignore:
Timestamp:
02/25/2010 09:06:44 PM (15 years ago)
Author:
ryan
Message:

Create a default menu containing the first 15 top-level pages. see #11817

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/nav-menus.php

    r13402 r13406  
    5757// Default Menu to show
    5858$custom_menus = wp_get_nav_menus();
     59
     60if ( empty($custom_menus)  && empty($_POST) ) {
     61    wp_create_default_nav_menu();
     62    $custom_menus = wp_get_nav_menus();
     63}
     64
    5965if ( ! $menu_selected_id && ! empty( $custom_menus ) )
    6066    $menu_selected_id = $custom_menus[0]->term_id;
  • trunk/wp-includes/nav-menu.php

    r13397 r13406  
    2222}
    2323
    24 function wp_create_nav_menu( $menu_name ) {
     24function wp_create_nav_menu( $menu_name, $args = array() ) {
    2525    $menu_exists = get_term_by( 'name', $menu_name, 'nav_menu' );
    2626
     
    2828        return new WP_Error('menu_exists', sprintf( __('A menu named “%s” already exists; please try another name.'), esc_html( $menu_exists->name ) ) );
    2929
    30     $menu = wp_insert_term( $menu_name, 'nav_menu' );
     30    if ( isset($args['slug']) )
     31        $slug = $args['slug'];
     32    else
     33        $slug = $menu_name;
     34
     35    $menu = wp_insert_term( $menu_name, 'nav_menu', array('slug' => $slug) );
     36
    3137    if ( is_wp_error($menu) )
    3238        return $menu;
    3339
    3440    return get_term( $menu['term_id'], 'nav_menu');
     41}
     42
     43function wp_create_default_nav_menu() {
     44    $menu = wp_create_nav_menu(__('Main'), array('slug' => 'main'));
     45
     46    if ( is_wp_error($menu) )
     47        return;
     48
     49    $pages = get_pages( array('parent' => 0, 'number' => 15) );
     50    $counter = 1;
     51    foreach ( $pages as $page ) {
     52        $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) );
     53        $item_id = wp_insert_post($item, true);
     54        update_post_meta( $item_id, 'menu_type', 'page' );
     55        update_post_meta( $item_id, 'object_id', $page->ID );
     56        update_post_meta( $item_id, 'menu_new_window', 0 );
     57        $counter++;
     58    }
    3559}
    3660
Note: See TracChangeset for help on using the changeset viewer.