Table of Contents
WordPress开发中自定义菜单的相关PHP函数使用简介,wordpressphp
您可能感兴趣的文章:
Home php教程 php手册 WordPress开发中自定义菜单的相关PHP函数使用简介,wordpressphp

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

Jun 13, 2016 am 08:48 AM
wordpress menu

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

函数意义
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会为没有设定菜单的导航里自动添加所有页面为导航,这是一个蛋疼的缺省,所以如果这个导航我们不想显示的话,你最好能在后天设定一个空的菜单,然后给不想显示的导航添加这个菜单,这样自定义导航就不会显示了。

您可能感兴趣的文章:

  • WordPress开发中用于获取近期文章的PHP函数使用解析
  • WordPress中用于获取搜索表单的PHP函数使用解析
  • 在WordPress中使用wp_count_posts函数来统计文章数量
  • 详解WordPress中调用评论模板和循环输出评论的PHP函数
  • 在WordPress中加入Google搜索功能的简单步骤讲解
  • 详解WordPress开发中的get_post与get_posts函数使用
  • 解析WordPress中的post_class与get_post_class函数
  • WordPress开发中的get_post_custom()函数使用解析
  • 在WordPress中安装使用视频播放器插件Hana Flv Player
  • 详解WordPress中分类函数wp_list_categories的使用
  • WordPress开发中短代码的实现及相关函数使用技巧
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

PHP vs. Flutter: The best choice for mobile development PHP vs. Flutter: The best choice for mobile development May 06, 2024 pm 10:45 PM

PHP and Flutter are popular technologies for mobile development. Flutter excels in cross-platform capabilities, performance and user interface, and is suitable for applications that require high performance, cross-platform and customized UI. PHP is suitable for server-side applications with lower performance and not cross-platform.

How to change page width in wordpress How to change page width in wordpress Apr 16, 2024 am 01:03 AM

You can easily modify your WordPress page width by editing your style.css file: Edit your style.css file and add .site-content { max-width: [your preferred width]; }. Edit [your preferred width] to set the page width. Save changes and clear cache (optional).

In which folder are wordpress articles located? In which folder are wordpress articles located? Apr 16, 2024 am 10:29 AM

WordPress posts are stored in the /wp-content/uploads folder. This folder uses subfolders to categorize different types of uploads, including articles organized by year, month, and article ID. Article files are stored in plain text format (.txt), and the filename usually includes its ID and title.

How to create a product page in wordpress How to create a product page in wordpress Apr 16, 2024 am 12:39 AM

Create a product page in WordPress: 1. Create the product (name, description, pictures); 2. Customize the page template (add title, description, pictures, buttons); 3. Enter product information (stock, size, weight); 4 . Create variations (different colors, sizes); 5. Set visibility (public or hidden); 6. Enable/disable comments; 7. Preview and publish the page.

Where is the wordpress template file? Where is the wordpress template file? Apr 16, 2024 am 11:00 AM

WordPress template files are located in the /wp-content/themes/[theme name]/ directory. They are used to determine the appearance and functionality of the website, including header (header.php), footer (footer.php), main template (index.php), single article (single.php), page (page.php), Archive (archive.php), category (category.php), tag (tag.php), search (search.php) and 404 error page (404.php). By editing and modifying these files, you can customize the appearance of your WordPress website

How to search for authors in WordPress How to search for authors in WordPress Apr 16, 2024 am 01:18 AM

Search for authors in WordPress: 1. Once logged in to your admin panel, navigate to Posts or Pages, enter the author name using the search bar, and select Author in Filters. 2. Other tips: Use wildcards to broaden your search, use operators to combine criteria, or enter author IDs to search for articles.

Which version of wordpress is stable? Which version of wordpress is stable? Apr 16, 2024 am 10:54 AM

The most stable WordPress version is the latest version because it contains the latest security patches, performance enhancements, and introduces new features and improvements. In order to update to the latest version, log into your WordPress dashboard, go to the Updates page and click Update Now.

What language is used to develop WordPress? What language is used to develop WordPress? Apr 16, 2024 am 12:03 AM

WordPress is developed using PHP language as its core programming language for handling database interactions, form processing, dynamic content generation, and user requests. PHP was chosen for reasons including cross-platform compatibility, ease of learning, active community, and rich library and frameworks. Apart from PHP, WordPress also uses languages ​​like HTML, CSS, JavaScript, SQL, etc. to enhance its functionality.

See all articles