Home Backend Development PHP Tutorial Introduction to the use of relevant PHP functions for custom menus in WordPress development_php tips

Introduction to the use of relevant PHP functions for custom menus in WordPress development_php tips

May 16, 2016 pm 08:00 PM
php wordpress Custom menu

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'
 
 ));
Copy after login

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 的菜单
<&#63;php wp_nav_menu( array( 'theme_location' => 'nav1' ) ); &#63;>
 
//调用 nav2 的菜单
<&#63;php wp_nav_menu( array( 'theme_location' => 'nav2' ) ); &#63;>
Copy after login

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">
Copy after login

How to filter it to make our web page source code concise and easy to read?

Add

to your functions.php
function 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');
Copy after login

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.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

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.

How to build a website for wordpress host How to build a website for wordpress host Apr 20, 2025 am 11:12 AM

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.

How to change the head image of the wordpress theme How to change the head image of the wordpress theme Apr 20, 2025 am 10:00 AM

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance &gt;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.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

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.

How to import the source code of wordpress How to import the source code of wordpress Apr 20, 2025 am 11:24 AM

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.

How to view the front-end of WordPress How to view the front-end of WordPress Apr 20, 2025 am 10:30 AM

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

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

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.

How to cancel the editing date of wordpress How to cancel the editing date of wordpress Apr 20, 2025 am 10:54 AM

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.

See all articles