Introduction to PHP functions related to custom menus in WordPress development, wordpressphp_PHP tutorial

Source: Internet
Author: User
Tags define function
Introduction to the use of PHP functions related to custom menus in WordPress development, wordpressphp. Introduction to PHP functions related to custom menus in WordPress development, wordpressphp function meaning wp_nav_menu () custom menu display function register_nav_menu () user-defined menu registration function WordPress development user-defined menu related PHP function usage introduction, wordpressphp

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.

    Articles you may be interested in:
    • Analysis of PHP functions used for obtaining recent articles in WordPress development
    • Use of PHP functions used to retrieve search forms in WordPress
    • Use the wp_count_posts function in WordPress to count the number of articles
    • Detailed description of PHP functions that call comment templates and output comments cyclically in WordPress
    • Simple steps to add the Google search function to WordPress
    • Details on the use of the get_post and get_posts functions in WordPress development
    • Parse post_class and get_post_class functions in WordPress
    • Using the get_post_custom () function in WordPress development
    • Install and use the video Player plug-in Hana Flv Player in WordPress
    • The use of the wp_list_categories function in WordPress
    • Implementation of short code in WordPress development and related function usage skills

    Define function meaning wp_nav_menu () custom menu display function register_nav_menu () custom menu registration function...

    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.