wp_create_nav_menu()创建一个新的前台导航菜单

目录

描述

Create a Navigation Menu. This function can be used to programmatically create a navigation menu which will be added to the Menus page under Appearance.

wp_create_nav_menu() 描述

用法

<?php $menu_id wp_create_nav_menu($menu_name?>

wp_create_nav_menu() 用法

参数

$menu_name

(string) (必填) The title of the menu to create.

默认值: None

wp_create_nav_menu() 参数

返回值

(int|WP_Error) 

The Menu ID or WP Error Object in case of failure.

wp_create_nav_menu() 返回值

示例

To check if a menu exists first and then create it if it doesn't exists, and finally add menu items to it, use:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

// Check if the menu exists
$menu_name = 'My First Menu';
$menu_exists = wp_get_nav_menu_object( $menu_name );

// If it doesn't exist, let's create it.
if( !$menu_exists){
    $menu_id = wp_create_nav_menu($menu_name);

	// Set up default menu items
    wp_update_nav_menu_item($menu_id, 0, array(
        'menu-item-title' =>  __('Home'),
        'menu-item-classes' => 'home',
        'menu-item-url' => home_url( '/' ), 
        'menu-item-status' => 'publish'));

    wp_update_nav_menu_item($menu_id, 0, array(
        'menu-item-title' =>  __('Custom Page'),
        'menu-item-url' => home_url( '/custom/' ), 
        'menu-item-status' => 'publish'));

}

wp_create_nav_menu() 示例

源文件

wp_create_nav_menu() 函数的代码位于 wp-includes/nav-menu.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Create a Navigation Menu.
 *
 * @since 3.0.0
 *
 * @param string $menu_name Menu name.
 * @return int|WP_Error Menu ID on success, WP_Error object on failure.
 */
function wp_create_nav_menu( $menu_name ) {
	return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
}

wp_create_nav_menu() 源文件

相关