This article describes how to use PHP functions related to custom menus in WordPress development, including filtering out useless class values in custom menus. For more information, see
Function meaning
Wp_nav_menu ()
Custom menu display function
Register_nav_menu ()
Custom menu registration function-singular
Register_nav_menus ()
Custom menu registration function-plural (a complex form that hurts a lot)
The following example uses register_nav_menus () to avoid confusion.
Usage location
Register_nav_menus ()
The function. php file is used to register the name of the code call in the background of the custom menu and the name displayed in the background.
Wp_nav_menu ()
Topic any location where you want to call the custom menu.
Usage
Register four menu corrections: three menus (sleepy, old error .)
Register_nav_menus (array ('nav1' => 'n1 ', // nav1 is the menu name (used when calling) // n1 is the display name, when selecting the menu in the background, use 'nav2' => 'n2 ', 'nav3' => 'n3 '));
Then go to your background and check the appearance = menu,
Basically, you understand,
If you do not understand,
Let's take another look ~
Add the following code to display the location of the custom navigation.
// Call the menu of nav1 <? Php wp_nav_menu (array ('theme _ location' => 'nav1');?> // Call the menu of nav2 <? Php wp_nav_menu (array ('theme _ location' => 'nav2');?>
Then you can add a menu in the background (the name should be a name or a Chinese character. Correction: preferably in English .)
You can set what is displayed in the menu, not displayed, or even set the level of the navigation.
Filter out useless CLASS values in the custom menu
In the output of the WordPress custom menu, each sub-element in the menu has a long class value, although there is no difference in appearance, but from the code perspective, it is really uncomfortable. In fact, there are many ways to filter out these class values. today, let's talk about how to mount a filter ), remove the classes we don't want.
In versions later than WordPress 3.5.0, the link feature will be removed and implemented using the custom menu feature.
When outputting a custom menu, WordPress outputs many class values to the sub-elements of the menu by default to allow us to better control the custom style, as shown below.
How can we filter out the source code of our webpage to make it concise and easy to read?
Add in your functions. php
Function filter_nav_menu_css_class ($ classes) {// The value of the class to be passed $ do_class_name = array ('current-menu-item'); $ outPut = array_intersect ($ do_class_name, $ classes ); array_filter ($ classes); return $ outPut;}; add_filter ('Nav _ menu_css_class ', 'filter _ nav_menu_css_class ');
Here, the current-menu-item value is retained mainly to identify the current page.
In fact,
There are other methods to filter the output class values,
For example, you can control the Walker_Nav_Menu class to precisely control the output of custom menus,
However, it is difficult to mention it here. In fact, the Walker_Nav_Menu class can complete more functions we need.
Conclusion-note
A functional concept introduced after WordPress 3.0 is similar to a previous tool. However, custom menus are more likely to be used for navigation, but the Chinese translation and circulation of new features are slow, in addition, as long as the topic is formed, bloggers who love to toss are generally not involved in navigation, so we still don't know about it, and even all of them see wrong tutorials, I hope that some bloggers will not repost their blog posts. even if some major functions are reposted, please test them to avoid misleading new users.
Note:
If the menu is not set for the custom navigation in the background settings, then wp will automatically add all pages as the navigation in the navigation without the set menu, so if we don't want to display this navigation, you 'd better set an empty menu the day after tomorrow, and then add this menu to the navigation that we don't want to display, so that the custom navigation will not be displayed.