WordPress开发中自定义菜单的相关PHP函数使用简介_PHP
函数意义
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' ));
然后去你的后台 =》 外观 =》 菜单 里面看下,
基本上你就明白了,
要是不明白,
那你就再看一下,嘿嘿~
主题你想要显示自定义导航的位置添加以下代码。
//调用 nav1 的菜单 <?php wp_nav_menu( array( 'theme_location' => 'nav1' ) ); ?> //调用 nav2 的菜单 <?php wp_nav_menu( array( 'theme_location' => 'nav2' ) ); ?>
然后就可以到后台去添加一个菜单了,(名称最好是名称或中文。更正:最好是英文。)
你可以设定这个菜单里面显示什么,不显示什么,甚至可以设置导航的层级内容,很方便的说。
过滤掉自定义菜单中无用的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">
如何过滤之,让我们的网页源代码变得简洁、易读呢?
在你的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');
这里将 current-menu-item 这个值保留主要是为了辨别当前页面。
其实,
还有其他方法去过滤输出的 class 值的,
比如说控制 Walker_Nav_Menu 这个类来精确控制自定义菜单的输出,
但因为比较麻烦所以在这里先不提,其实 Walker_Nav_Menu 这个类可以完成更多我们需要的功能。
总结 – 需注意
自定义菜单式在 WordPress 3.0 后才引入的一个功能概念,有点类似于之前的小工具,不过自定义菜单更倾向于导航用途,但因为新功能的中文翻译、流传较慢,再加上这个功能只要是主题成型的爱折腾的博主一般不会在导航方面折腾,所以大家还一直不了解,甚至都出现了错误教程满天飞得局面,在这里希望有的博主在发表博文的时候不要以转载为主,就算是转载一些主要功能请具体测试一下,以免误导新人。
需注意:
后台设置中如果没有给自定义导航里面设定菜单的话,那wp会为没有设定菜单的导航里自动添加所有页面为导航,这是一个蛋疼的缺省,所以如果这个导航我们不想显示的话,你最好能在后天设定一个空的菜单,然后给不想显示的导航添加这个菜单,这样自定义导航就不会显示了。

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

AI Hentai Generator
Generate AI Hentai for free.

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Wordpress site file access is restricted: troubleshooting the reason why .txt file cannot be accessed recently. Some users encountered a problem when configuring the mini program business domain name: �...
