如何为我的主题创建预定义菜单?

时间:2012-04-25 作者:Mythical Fish

这是我迄今为止的代码:

function register_my_menus() {
    register_nav_menus(
        array(
             \'header-menu\' => __( \'Header Menu\' ),
             \'extra-menu\' => __( \'Extra Menu\' )
        )
    );
}
add_action( \'init\', \'register_my_menus\' );
但这似乎只注册放置自定义菜单的“位置”。我想要的是所有自定义菜单(和菜单项)都可以完全设置after_theme_setupinit.

谢谢

2 个回复
最合适的回答,由SO网友:Adam 整理而成

示例代码取自new2wp.com 位于HERE

   // Function for registering wp_nav_menu() in 3 locations
    add_action( \'init\', \'register_navmenus\' );
    function register_navmenus() {
        register_nav_menus( array(
            \'Top\'       => __( \'Top Navigation\' ),
            \'Header\'    => __( \'Header Navigation\' ),
            \'Footer\'    => __( \'Footer Navigation\' ),
            )
        );

        // Check if Top menu exists and make it if not
        if ( !is_nav_menu( \'Top\' )) {
            $menu_id = wp_create_nav_menu( \'Top\' );
            $menu = array( \'menu-item-type\' => \'custom\', \'menu-item-url\' => get_home_url(\'/\'),\'menu-item-title\' => \'Home\' );
            wp_update_nav_menu_item( $menu_id, 0, $menu );
        }
        // Check if Header menu exists and make it if not
        if ( !is_nav_menu( \'Header\' )) {
            $menu_id = wp_create_nav_menu( \'Header\' );
            $menu = array( \'menu-item-type\' => \'custom\', \'menu-item-url\' => get_home_url(\'/\'), \'menu-item-title\' => \'Home\' );
            wp_update_nav_menu_item( $menu_id, 0, $menu );
        }
        // Check if Footer menu exists and make it if not
        if ( !is_nav_menu( \'Footer\' )) {
            $menu_id = wp_create_nav_menu( \'Footer\' );
            $menu = array( \'menu-item-type\' => \'custom\', \'menu-item-url\' => get_home_url(\'/\'), \'menu-item-title\' => \'Home\' );
            wp_update_nav_menu_item( $menu_id, 0, $menu );
        }

        // Get any menu locations that dont have a menu assigned to it and give it on
        /* Currently not working. couldnt fix it.
        $loc = array(\'Top\', \'Header\', \'Footer\');
        if ( has_nav_menu( $location )) {
            $locations = get_nav_menu_locations();
            return (!empty( $locations[ $location ] ));
        }
        */
    }
    /* Delete nav menu in case you need it
    wp_delete_nav_menu( $menu );
    */

SO网友:Mythical Fish

对于寻求相同解决方案的任何人,以下是代码:

function register_my_menus() {

    $menus = array(
        \'Main menu\' => array(
            \'slug\' => \'main-menu\',
            \'menu_items\' => array(
                \'Home\' => site_url(),
                \'Search\' => site_url(\'/search/\'),
                \'Online now\' => site_url(\'online-now\'),
                \'Video Chat\' => site_url(\'/video-chat/\'),
                \'Galleries\' => site_url(\'/galleries/\'),
                \'Forums\' => site_url(\'/forums/\'),
                \'Live cams\' => site_url(\'/live-cams/\')
            )
        )
    );

    foreach($menus as $menu_title => $menu_var) {
        register_nav_menu( $menu_var[\'slug\'], $menu_title );
        if( !is_nav_menu($menu_title) ) {
            $menu_id = wp_create_nav_menu( $menu_title );
            foreach( $menu_var[\'menu_items\'] as $menu_item_name => $menu_item_url ) {
                $item = array ( \'menu-item-type\' => \'custom\', \'menu-item-url\' => $menu_item_url, \'menu-item-title\' => $menu_item_name );
                wp_update_nav_menu_item( $menu_id, 0, $item );
            }
        }
     }
}

add_action( \'init\', \'register_my_menus\' );
您只需修改$menus 阵列:)

结束

相关推荐

Menu API not switching menus?

我正在使用菜单API,我想切换到其他菜单,但出于某种原因,它保留了第一个菜单这是我的密码在函数中。php add_action( \'init\', \'register_my_menus\',10 ); function register_my_menus() { register_nav_menu(\'main-navigation\', \'Main Navigation\'); } 下面是我的主题文件(header.ph