wp_create_nav_menu() - WordPress 函数参考中文文档

返回介绍

wp_create_nav_menu()

发布于 2017-09-11 字数 2386 浏览 1106 评论 0

wp_create_nav_menu( string $menu_name )

Creates a navigation menu.


描述

Note that $menu_name is expected to be pre-slashed.


参数

$menu_name

(string)
(Required)
Menu name.


返回值

(int|WP_Error) Menu ID on success, WP_Error object on failure.


源代码

File: wp-includes/nav-menu.php

function wp_create_nav_menu( $menu_name ) {
	// expected_slashed ($menu_name)
	return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
}

更新日志

Version 描述
3.0.0 Introduced.

相关函数

Uses

  • wp-includes/nav-menu.php:
    wp_update_nav_menu_object()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Basic Example

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

    
    // 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', 'textdomain' ),
            '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', 'textdomain' ),
            'menu-item-url'    => home_url( '/custom/' ), 
            'menu-item-status' => 'publish'
    	) );
    }
    

上一篇:wp_create_category()

下一篇:wp_create_nonce()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!