


Introduction to the use of relevant PHP functions for custom menus in WordPress development_php tips
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 very painful plural form)
The following examples use the plural form of register_nav_menus () to avoid confusion.
Use location
register_nav_menus ()
In the function.php file, it is used to register the code call name and background display name of the custom menu in the background.
wp_nav_menu ()
Theme anywhere you want to call your custom menu.
How to use
Register 4 menus Correction: 3 menus (sleepy, always make mistakes.)
register_nav_menus( array( 'nav1' => 'n1', //nav1 为菜单名称(调用的时候用) //n1 为显示名称 , 后台选菜单的时候用 'nav2' => 'n2', 'nav3' => 'n3' ));
Then go to your backend => Appearance => Menu and look inside,
Basically you get it,
If you don’t understand,
Then take a look again, hehe~
Add the following code to the theme where you want to display custom navigation.
//调用 nav1 的菜单 <?php wp_nav_menu( array( 'theme_location' => 'nav1' ) ); ?> //调用 nav2 的菜单 <?php wp_nav_menu( array( 'theme_location' => 'nav2' ) ); ?>
Then you can go to the backend to add a menu, (the name is preferably a name or Chinese. Correction: preferably English.)
You can set what is displayed and what is not displayed in this menu, and you can even set the hierarchical content of the navigation, which is very convenient.
Filter out useless CLASS values in custom menus
In the output of WordPress’s custom menu, each sub-element in the menu has a long class value. Although there is nothing unusual in appearance, it does make people feel uncomfortable when looking at the code. , in fact, there are many ways to filter out these class values. Today we will talk about removing the classes we don’t want by mounting a filter.
In WordPress 3.5.0 and later versions, the friendly link function will be canceled and implemented with a custom menu function. This is a story later.
When we output a custom menu, in order to give us better control over the custom style, WordPress will output multiple class values for the sub-elements of our menu by default, like the following.
<li id="menu-item-19" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-19">
How to filter it to make our web page source code concise and easy to read?
Add
to your functions.phpfunction filter_nav_menu_css_class ($classes){ //想要通过的class值 $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');
The value of current-menu-item is retained here mainly to identify the current page.
Actually,
There are other ways to filter the output class value,
For example, control the Walker_Nav_Menu class to precisely control the output of the custom menu,
But because it is more troublesome, I won’t mention it here. In fact, the Walker_Nav_Menu class can complete more functions we need.
Summary – Notes
Custom menu is a functional concept introduced only after WordPress 3.0. It is somewhat similar to the previous widgets, but the custom menu is more inclined to navigation purposes. However, due to the slow Chinese translation and spread of the new function, and the Bloggers who like to mess around with this function as long as the theme is formed generally won't bother with navigation, so everyone still doesn't understand it, and there are even situations where wrong tutorials are flying all over the sky. Here I hope some bloggers will post blog posts. Don't focus on reprinting. Even if you reprint some of the main functions, please test them in detail to avoid misleading newcomers.
Note:
If there is no menu set for the custom navigation in the background settings, then wp will automatically add all pages as navigation to the navigation without setting the menu. This is a painful default, 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 you don't want to display, so that the custom navigation will not be displayed.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance >Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

You can view the WordPress front-end by logging into the dashboard and switching to the View Sites tab; automate the viewing process with a headless browser; installing the WordPress plugin to preview the front-end within the dashboard; viewing the front-end via a local URL (if WordPress is set locally).

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.
