PHP core method for creating navigation menu in WordPress _ php instance

Source: Internet
Author: User
This article mainly introduces the PHP core method for creating navigation menus in WordPress, that is, the function and usage of parameters related to wp_get_nav_menu. If you need it, refer to WordPress 3.0 to introduce the navigation menu function, it makes page navigation and link management easy to use. WP provides you with a menu management page and multiple call methods. Let's first take a look at what functions are available in the WordPress navigation menu.

Manage menu page

Page path: Appearance> Menus
From the perspective of the interface, you can create multiple menus and add custom links, page links, and Category links to the menu.

However, note that if you add page links and Category links, only links are included in the menu, rather than pages and categories. That is to say, subpages and subcategories are not part of menus.

In addition, we cannot add other menus to the menu, so this custom menu is destined to have only one level. in the menu bar on the right, you only need to move a menu to the right to serve as a lower menu. therefore, you can create multiple levels.
After creating a menu, we can add these menus to the sidebar on the Widgets page.

Register custom menu

On the menu management page, notice the Theme Locations block prompt as follows:

The current theme does not natively support menus, but you can use the "Custom Menu" widget to add any menus you create here to the theme's sidebar.

This means that your topic does not support custom menus, but you can add them to the sidebar of the topic by using widgets. How can you enable the topic to support custom menus? Add the following code to function. php.

register_nav_menus(array( 'primary' => 'Primary Navigation'));

This code is used to record a custom menu. You can select a specific application menu for it. primary should be the unique identifier, and Primary Navigation is the name of the menu. you can use this function to add multiple custom menus to a topic. if. the php registration method is as follows:

register_nav_menus(array('primary' => 'Primary Navigation'));register_nav_menus(array('secondary' => 'Secondary Navigation'));register_nav_menus(array('bottom' => 'Bottom Navigation'));

The content shown in figure is displayed on the menu management page.

Topic call menu

How can I call a menu in a topic? Use wp_nav_menu (); in the appropriate position of the topic to output the menu to the page.
The theme_location parameter is provided in the method to specify the corresponding custom menu. To call the first menu, the Code is as follows:

wp_nav_menu(array( 'theme_location' =>'primary'));

By default, if no menu is defined, the wp_nav_menu method is the same as the wp_list_pages (call page list) method, but it is less efficient than the latter. therefore, if you want to use the page list or category list as the navigation menu, we recommend that you do not use wp_nav_menu.


The method for calling the navigation menu on a topic is simple. You only need to add the following statements to output the menu on the page.

<?php wp_nav_menu(); ?>

However, this method provides many configurable parameters. We will describe them one by one.


The parameter list is from WordPress Codex. The parameters are translated one by one and described in detail.

$ Menu
(String) (optional) the menu to be displayed; accept (in order) id, slug, name
Default Value: None

Let's take a look at the WordPress menu retrieval method. Like the description on Codex, it is obtained in the order of id, slug, and name.

Function wp_get_nav_menu_object ($ menu) {// if (! $ Menu) return false; // find $ menu_obj = get_term ($ menu, 'nav _ menu ') by id; // if no value is found, locate if (! $ Menu_obj) $ menu_obj = get_term_by ('slug', $ menu, 'nav _ menu '); // if no result is found, find if (! $ Menu_obj) $ menu_obj = get_term_by ('name', $ menu, 'nav _ menu '); // if (! $ Menu_obj) $ menu_obj = false; return $ menu_obj ;}

$ Container
(String) (optional) label type of ul parent node
Default Value: p

Do not think that any label can be used. In fact, only p and nav will be used. If other values are entered, the label of the ul parent node will not be displayed. The description of Codex is not detailed enough. (from another perspective, WordPress uses the nav tag to demonstrate that it is improving its support for HTML5 .)

// Only p and nav $ allowed_tags = apply_filters ('wp _ nav_menu_container_allowedtags ', array ('P', 'nav') are allowed to be used '));

$ Container_class
(String) (optional) class Attribute Value of ul parent node
Default Value: menu-{menu slug}-container

$ Container_id
(String) (optional) id attribute value of ul parent node
Default Value: None

$ Menu_class
(String) (optional) class Attribute Value of ul Node
Default Value: menu

$ Menu_id
(String) (optional) id attribute value of the ul Node
Default Value: menu slug, auto-Increment

$ Echo
(Boolean) (optional) determines whether to directly display the menu or return HTML clips.
Default Value: true (direct display)

$ Fallback_cb
(String) (optional) callback function called if the menu does not exist
Default Value: wp_page_menu (display page list as menu)

This is a very important method. It can be used to ensure compatibility with the themes of earlier versions. next let's look at the code. the key is that $ args is also passed into call_user_func. for example, if we write the parameter 'sort _ column' => 'menu _ order' into the parameter wp_nav_menu, it will also be passed to the call_user_func method. if call_user_func is the wp_page_menu method, the displayed page list is sorted and output according to the assigned sequence number.

// If the specified menu cannot be found, or no menu exists and no custom menu is specified, use the call_user_func method to process if ((! $ Menu | is_wp_error ($ menu) | (isset ($ menu_items) & empty ($ menu_items )&&! $ Args-> theme_location) & (function_exists ($ args-> fallback_cb) | is_callable ($ args-> fallback_cb) return call_user_func ($ args-> fallback_cb, (array) $ args );

$ 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

I suspect that Codex's descriptions of $ before and $ link_before, $ after, and $ link_after are reversed?

$ Depth
(Integer) (optional) shows the menu depth. When the value is 0, all
Default Value: 0

$ Walker
(String) (optional) custom traversal object
Default Value: None

$ Theme_locaton
(String) (optional) the location in the theme to be used -- must be registered with register_nav_menu () in order to be selectable by the user
Default Value: None

If the topic registers three custom menus in function. php:

register_nav_menus(array('primary' => 'Primary Navigation'));register_nav_menus(array('secondary' => 'Secondary Navigation'));register_nav_menus(array('bottom' => 'Bottom Navigation'));

To call the Navigation menu Secondary Navigation, you can use the following statement in the header. php file:

wp_nav_menu(array( 'theme_location' =>'secondary'));

That is to say, this is used to specify to call a custom menu.

Related Article

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: 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.