The PHP core method of making navigation menu in WordPress explains _php example

Source: Internet
Author: User

WordPress 3.0 introduces the navigation menu function, lets the page navigation and the link management become simple and easy to use. WP to provide users with menu Management page and a variety of call methods, we first look at the general WordPress navigation menu have what function.


Manage Menu Pages

Page path: appearance > Menus
From the interface, you can create multiple menus, and you can add custom links, page links, and category links to the menu.

But one thing to note is that if you add page links and category links, the only links that are brought into the menu are the link, not the page and the category itself, that is, the subpages and subcategories are not part of the menu.

In addition, we can not add other menus to the menu, so this custom menu is destined to exist only one level. In a menu block on the right, you can serve as a subordinate menu by simply moving a menu to the right. So it is possible to create multilevel.
After creating the menu, we can add the menus to the sidebar by adding them to the Widgets page.

Registering Custom Menus

Note In the menu Management page that the Theme locations block tip is as follows:

The current theme does is not natively support menus, but your can use the ' Custom Menu ' widget to ' Add any menus ' create he Re to the theme ' s sidebar.

It means that your theme does not support customizing menus, but you can add customizations to the sidebar of the theme in the Widget way. So how do you make themes support custom menus? Please add the following code to the function.php.

Register_nav_menus (Array (
 ' primary ' => ' primary navigation '
));

This code is used to record a custom menu, and you can choose a specific application menu for it, where primary should be a unique identifier, primary navigation is the name of the menu. You can use this function to add multiple custom menus to a theme. If you add a registration method to function.php, the following:

Register_nav_menus (Array (' primary ' => ' primary navigation '));
Register_nav_menus (Array (' secondary ' => ' secondary navigation '));
Register_nav_menus (Array (' Bottom ' => ' bottom navigation '));

On the menu Management page you will see the following figure.

Theme Invoke Menu

How do you invoke a menu in a topic? Use the method Wp_nav_menu () at the appropriate location of the theme, and output the menu to the page.
method to specify the corresponding custom menu by providing the parameter theme_location. To invoke the first menu, the code is as follows:

Wp_nav_menu (Array (
 ' theme_location ' => ' primary '
));

By default, if no menus are defined, using the Wp_nav_menu method is no different from the Wp_list_pages (calling page list) method, but less efficient. Therefore, if you are ready to use the page list or category list as the navigation menu, it is recommended not to use Wp_nav_menu.

Usage

Calling the navigation menu in a theme is simple. Simply add the following statement to output the menu on the page.

<?php Wp_nav_menu ();?>

But in fact this method provides a lot of configurable parameters, as we described below.

Parameters

The list of parameters is from WordPress Codex, translated below, and detailed descriptions of the parameters that are difficult to understand.

$menu
(optional) The menu that is expected to be displayed; Accept (sequentially matched) IDs, Slug, name
Default value: None

Let's look at the way WordPress takes the menu. Like the description on Codex, it is taken in the order of ID, slug, name.

function Wp_get_nav_menu_object ($menu) {
 //No arguments supplied, returns an empty
 if (! $menu) return
 false;
 
 Find
 $menu _obj = get_term ($menu, ' Nav_menu ') based on ID;
 
 If not found, according to Slug to find if
 (! $menu _obj)
 $menu _obj = get_term_by (' Slug ', $menu, ' Nav_menu ');
 
 If you can't find it, then look for
 if (! $menu _obj)
 $menu _obj = get_term_by (' name ', $menu, ' Nav_menu ') according to name;
 
 Finally did not find, returned an empty
 if (! $menu _obj)
 $menu _obj = false;
 
 return $menu _obj;
}

$container
(optional) The label type of the UL parent node
Default value: div

Do not think that any label can be used, in fact only div and NAV will be adopted, if the input of other values, the UL of the parent node will not show the label, the description of the Codex is not exhaustive. (from another point of view, WordPress uses the nav tag to show that it is boosting support for HTML5.)

The labels allowed are only div and nav
$allowed _tags = apply_filters (' wp_nav_menu_container_allowedtags ', array (' div ', ' nav '));

$container _class
(optional) UL parent node's class attribute value
Default value: Menu-{menu Slug}-container

$container _id
(optional) UL parent Node ID attribute value
Default value: None

$menu _class
(optional) The class attribute value of the UL node
Default Value: Menu

$menu _id
(optional) The id attribute value of the UL node
Default value: Menu slug, self-growing

$echo
(Boolean) (optional) Decide whether to display the menu directly or return an HTML fragment
Default value: True (direct display)

$fallback _CB
(optional) If the menu does not exist, the calling callback function
Default value: Wp_page_menu (Display page list as menu)

This is a very important method that can be used to compatible with the old version of the theme. Let's look at the code below. The key is that $args is also passed into the call_user_func. For example, we write the parameter ' sort_column ' => ' Menu_order ' to the Wp_nav_menu parameter, and it is also uploaded to the Call_user_func method. If Call_user_func is the Wp_page_menu method, the list of pages displayed will sort the output by the ordinal that is assigned.

If the specified menu is not found, or if the menu does not have any entries and does not specify a custom menu, the Call_user_func method is used 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
(optional) the text to display before each menu link
Default value: None

$after
(optional) Displays the text after each menu link
Default value: None

$link _before
(optional) text that appears before each menu link text
Default value: None

$link _after
(optionally) the text that appears after each menu link text
Default value: None

I suspect that Codex $before and $link _before, $after and $link _after's description is upside down?

$depth
(int) (optional) Displays the depth of the menu, when the value is 0 o'clock to show all
Default value: 0

$walker
(optional) Custom Traversal object
Default value: None

$theme _locaton
(String) (optional) The location in the theme to is Used--must is registered with Register_nav_menu By the user
Default value: None

If the topic has 3 custom menus registered in function.php, the following:

Register_nav_menus (Array (' primary ' => ' primary navigation '));
Register_nav_menus (Array (' secondary ' => ' secondary navigation '));
Register_nav_menus (Array (' Bottom ' => ' bottom navigation '));

To invoke secondary navigation this navigation menu, you can use the following statement in the header.php file:

Wp_nav_menu (Array (
 ' theme_location ' => ' secondary '
));

In other words, this is used to specify that a custom menu is invoked.

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.