Home Backend Development PHP Tutorial WordPress开发中自定义菜单的相关PHP函数使用简介_PHP

WordPress开发中自定义菜单的相关PHP函数使用简介_PHP

May 28, 2016 am 11:49 AM
php wordpress Custom menu

函数意义
wp_nav_menu ()
自定义菜单显示函数

register_nav_menu ()
自定义菜单注册函数 – 单数

register_nav_menus ()
自定义菜单注册函数 – 复数 (很蛋疼的一个复数形式)

以下举例均用 register_nav_menus () 这个复数形式,以免混淆。

使用位置
register_nav_menus ()
function.php 文件中,用于注册自定义菜单在后台的代码调用名称和后台显示名称。

wp_nav_menu ()
主题任意你想调用自定义菜单的位置。

使用方法
注册4个菜单更正:3个菜单(困了,老出错。)

 register_nav_menus( array( 
 'nav1' => 'n1',
//nav1 为菜单名称(调用的时候用)
//n1 为显示名称 , 后台选菜单的时候用
 
 'nav2' => 'n2',
 
 'nav3' => 'n3'
 
 ));
Copy after login

然后去你的后台 =》 外观 =》 菜单 里面看下,
基本上你就明白了,
要是不明白,
那你就再看一下,嘿嘿~

主题你想要显示自定义导航的位置添加以下代码。


//调用 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

然后就可以到后台去添加一个菜单了,(名称最好是名称或中文。更正:最好是英文。)
你可以设定这个菜单里面显示什么,不显示什么,甚至可以设置导航的层级内容,很方便的说。

过滤掉自定义菜单中无用的CLASS值
在 WordPress 的自定义菜单的输出中,菜单中每一个子元素都有一个很长的 class 值,虽然从外观上看不出什么异样来,但从代码上看确实让人心里很难受,其实要过滤掉这些 class 值有很多方法,今天来讲一下通过挂载过滤器 (filter),去除那些我们不想要的class。
在 WordPress 3.5.0 以后的版本中将取消友情链接功能,并以自定义菜单功能实现,这个是后话。
我们在输出自定义菜单的时候,为了让我们更好的控制自定义样式, WordPress 会默认给我们菜单的子元素输出很多个 class 的值,就像下面这样。

<li id="menu-item-19" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-19">
Copy after login

如何过滤之,让我们的网页源代码变得简洁、易读呢?

在你的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

这里将 current-menu-item 这个值保留主要是为了辨别当前页面。

其实,
还有其他方法去过滤输出的 class 值的,
比如说控制 Walker_Nav_Menu 这个类来精确控制自定义菜单的输出,
但因为比较麻烦所以在这里先不提,其实 Walker_Nav_Menu 这个类可以完成更多我们需要的功能。

总结 – 需注意
自定义菜单式在 WordPress 3.0 后才引入的一个功能概念,有点类似于之前的小工具,不过自定义菜单更倾向于导航用途,但因为新功能的中文翻译、流传较慢,再加上这个功能只要是主题成型的爱折腾的博主一般不会在导航方面折腾,所以大家还一直不了解,甚至都出现了错误教程满天飞得局面,在这里希望有的博主在发表博文的时候不要以转载为主,就算是转载一些主要功能请具体测试一下,以免误导新人。

需注意:
后台设置中如果没有给自定义导航里面设定菜单的话,那wp会为没有设定菜单的导航里自动添加所有页面为导航,这是一个蛋疼的缺省,所以如果这个导航我们不想显示的话,你最好能在后天设定一个空的菜单,然后给不想显示的导航添加这个菜单,这样自定义导航就不会显示了。

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 尊渡假赌尊渡假赌尊渡假赌

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
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
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 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).

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.

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