How to register and call menus in WordPress

Source: Internet
Author: User
This article describes how to register a menu and call a menu in WordPress. the register_nav_menus () function and wp_nav_menu () function are used respectively. For more information, see Register_nav_menus () (registration menu)
The register_nav_menus () function is used to register a menu. the menu refers to the WordPress 3.0 + menu manager. After registration, you can drag the menu in the menu manager to generate a navigation menu.

Usage

register_nav_menus( $locations );

Parameters

$ Locations

(Array) (required) name of the menu to be registered. The key value is the menu ID and the key name is the menu name. you can create multiple menus at a time.

Default value: None

Return value

This function has no return value.

Example

/*** Create a menu ** using Bing_register_nav_menus () {register_nav_menus (array ('header _ menu '=> _ ('top menu', 'Bing '), 'footer _ menu '=> _ ('footer menu', 'Bing ');} add_action ('init', 'Bing _ register_nav_menus ');

Others

This function is located in: wp-des/nav-menu.php


Wp_nav_menu () (menu call)
The following describes how to call the menu after adding a user.

The wp_nav_menu () function is used to call the menu. the wp_nav_menu () function has many parameters, so the function is very powerful. here we will explain it one by one.

Usage

wp_nav_menu( $args );

Parameters

There is only one $ args parameter, but this is an array. you can customize more details by adding parameters to the array. the default value is below:


$defaults = array(  'theme_location' => '',  'menu'      => '',  'container'    => 'p',  'container_class' => '',  'container_id'  => '',  'menu_class'   => 'menu',  'menu_id'     => '',  'echo'      => true,  'fallback_cb'   => 'wp_page_menu',  'before'     => '',  'after'      => '',  'link_before'   => '',  'link_after'   => '',  'items_wrap'   => '
 
 
    %3$s
', 'depth' => 0, 'walker' => '');wp_nav_menu( $defaults );

Details:

Theme_locaton

(String) (optional) name of the menu to be called, such as header_menu. if not specified, the first registered menu is displayed by default.

Default value: None

Menu

(String) (optional) use the name of the navigation menu to call the menu, which can be ID, alias, and name (matching in order ).

Default value: None

Container

(String) (optional) The container type of the navigation menu. only the p and nav labels are supported. if it is another value, the label of the ul parent node is not displayed. You can also set False to remove ul parent nodes.

Default value: p

Container_class

(String) (optional) class attribute of ul parent node.

Default value: menu-{menu slug}-container

Container_id

(String) (optional) id attribute of the ul parent node.

Default value: None

Menu_class

(String) (optional) class attribute of the ul node.

Default value: None

Menu_id

(String) (optional) id attribute of the ul node.

Default value: menu alias

Echo

(Boolean) (optional) return to the Html code of the navigation menu or print the output directly. if you want to save the navigation menu code to the variable, set it to False.

Default value: True (print output directly)

Fallback_cb

(Callback function) (optional) content displayed by default when this menu is not set in the background.

Default value: wp_page_menu

Before

(String) (optional) text displayed before each menu link.

Default value: None

After

(String) (optional) text displayed after each menu link.

Default value: None

Link_before

(String) (optional) text displayed before the link text of each menu.

Default value: None

Link_after

(String) (optional) text displayed after the link text of each menu.

Default value: None

Items_wrap

(String) (optional) replace ul's class attributes.

Default value: None

Depth

(Integer) (optional)

Displays the menu depth. when the value is 0, all menus of depth are displayed.

Default value: 0

Walker

(Object) (optional) structure object of the menu.

Default value: new Walker_Nav_Menu

Example

<?php wp_nav_menu( array( 'theme_location' => 'header_menu', 'container' => false, 'items_wrap' => '
 
 
    %3$s
', 'fallback_cb' => 'Bing_menu_null_fallback' ) ); ?>

Others

This function is located in: wp-des/nav-menu-template.php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.