getdave / Tanlinell

Boilerplate Wordpress theme for rapid development of new WP themes. Based on the great work of the _s ("Underscore") theme.
GNU General Public License v2.0
6 stars 2 forks source link

Work further on default menu setup #190

Closed neilberry001 closed 10 years ago

neilberry001 commented 11 years ago

WIP

<?php
/**
 * Default Menu Setup
 * 
 * @package Tanlinell
 * @since Tanlinell 1.0
 */

function tanlinell_default_menu_setup() {

    $name = 'Primary Navigation';

    $menu_exists = wp_get_nav_menu_object( $name );

    if( !$menu_exists ) {
        $menu_id = wp_create_nav_menu($name);

        //then get the menu object by its name
        $menu = get_term_by( 'name', $name, 'nav_menu' );

        $args = array(
            'menu-item-title' =>  __('Home'),
            'menu-item-classes' => 'menu-item--home',
            'menu-item-url' => home_url( '/' ), 
            'menu-item-status' => 'publish',
            'menu-item-parent-id' => 0,
        );

        wp_update_nav_menu_item( $menu->term_id, 0, $args );

        $args = array(
            'menu-item-title' =>  __('News'),
            'menu-item-classes' => 'menu-item--news',
            'menu-item-url' => home_url( '/news/' ), 
            'menu-item-status' => 'publish',
            'menu-item-parent-id' => 0,
        );

        wp_update_nav_menu_item( $menu->term_id, 0, $args );

        $locations = get_theme_mod('nav_menu_locations');
        $locations['primary'] = $menu->term_id;
        set_theme_mod( 'nav_menu_locations', $locations );
    }

    $name = 'Footer Navigation';

    $menu_exists = wp_get_nav_menu_object( $name );

    if( !$menu_exists ) {
        $menu_id = wp_create_nav_menu($name);

        //then get the menu object by its name
        $menu = get_term_by( 'name', $name, 'nav_menu' );

        $args = array(
            'menu-item-title' =>  __('Home'),
            'menu-item-classes' => 'menu-item--home',
            'menu-item-url' => home_url( '/' ), 
            'menu-item-status' => 'publish',
            'menu-item-parent-id' => 0,
        );

        wp_update_nav_menu_item( $menu->term_id, 0, $args );

        $args = array(
            'menu-item-title' =>  __('News'),
            'menu-item-classes' => 'menu-item--news',
            'menu-item-url' => home_url( '/news/' ), 
            'menu-item-status' => 'publish',
            'menu-item-parent-id' => 0,
        );

        wp_update_nav_menu_item( $menu->term_id, 0, $args );

        $locations = get_theme_mod('nav_menu_locations');
        $locations['subsidiary'] = $menu->term_id;
        set_theme_mod( 'nav_menu_locations', $locations );
    }

}
add_action( 'after_switch_theme', 'tanlinell_default_menu_setup' );
?>
neilberry001 commented 10 years ago

see http://wpseek.com/wp_update_nav_menu_item/ for wp_update_nav_menu_item definition in wp-includes/nav-menu.php testing required for adding param:

'menu-item-type' => 'post_type',

menu item menu-item-object-id needs consideration