search
HomeBackend DevelopmentPHP TutorialWordPress中注册菜单与调用菜单的方法详解_php技巧

register_nav_menus()(注册菜单)
register_nav_menus() 函数用来注册一个菜单,菜单指的是 WordPress 3.0+ 的菜单管理器,注册之后用户就可以在菜单管理器里拖动生成导航菜单了。

用法

register_nav_menus( $locations );

参数

$locations

(数组)(必须)要注册的菜单,键值为菜单 ID,键名为菜单名称,可以一次创建多个。

默认值:None

返回值

该函数无返回值。

例子

/**
  *建立菜单
  *http://www.endskin.com/register_nav_menus/
*/
function Bing_register_nav_menus(){
  register_nav_menus( array(
    'header_menu' => __( '顶部菜单', 'Bing' ),
    'footer_menu' => __( '页脚菜单', 'Bing' )
  ));
}
add_action( 'init', 'Bing_register_nav_menus' );

其它

该函数位于:wp-includes/nav-menu.php


wp_nav_menu()(菜单调用)
下面就来说一下怎么调用用户添加好后的菜单。

调用菜单主要使用 wp_nav_menu() 函数,wp_nav_menu() 函数的参数比较多,所以功能非常强大,这里会一个一个的讲解。

用法

wp_nav_menu( $args );

参数

参数只有一个 $args,但这是一个数组,通过给数组添加参数,可以定制更多细节,下边是默认值:


$defaults = array(
  'theme_location' => '',
  'menu'      => '',
  'container'    => 'div',
  'container_class' => '',
  'container_id'  => '',
  'menu_class'   => 'menu',
  'menu_id'     => '',
  'echo'      => true,
  'fallback_cb'   => 'wp_page_menu',
  'before'     => '',
  'after'      => '',
  'link_before'   => '',
  'link_after'   => '',
  'items_wrap'   => '<ul id="%1$s" class="%2$s">%3$s</ul>',
  'depth'      => 0,
  'walker'     => ''
);
wp_nav_menu( $defaults );

详解:

theme_locaton

(字符串)(可选)要调用的菜单的名字,比如 header_menu,如果没指定,则默认显示第一个注册的菜单。

默认值:None

menu

(字符串)(可选)使用导航菜单的名称调用菜单,可以是 ID、别名和名字(按顺序匹配)。

默认值:None

container

(字符串)(可选)导航菜单的容器类型,只支持 div 和 nav 标签,如果是其它值,ul 父节点的标签将不会显示。也可以设置成 False 去掉 ul 父节点。

默认值:div

container_class

(字符串)(可选)ul 父节点的 class 属性。

默认值:menu-{menu slug}-container

container_id

(字符串)(可选)ul 父节点的 id 属性。

默认值:None

menu_class

(字符串)(可选)ul 节点的 class 属性。

默认值:None

menu_id

(字符串)(可选)ul 节点的 id 属性。

默认值:菜单别名

echo

(布尔)(可选)返回导航菜单的 Html 代码还是直接打印输出,如果你想把导航菜单代码存到变量里请设置成 False.

默认值:True(直接打印输出)

fallback_cb

(回调函数)(可选)后台没有设置此菜单时默认显示的内容。

默认值:wp_page_menu

before

(字符串)(可选)显示在每个菜单链接前的文本。

默认值:None

after

(字符串)(可选)显示在每个菜单链接后的文本。

默认值:None

link_before

(字符串)(可选)显示在每个菜单链接文本前的文本。

默认值:None

link_after

(字符串)(可选)显示在每个菜单链接文本后的文本。

默认值:None

items_wrap

(字符串)(可选)替换 ul 的 class 属性。

默认值:None

depth

(整形)(可选)

显示菜单的深度,当数值为 0 时显示所有深度的菜单。

默认值:0

walker

(对象)(可选)菜单的结构对象。

默认值:new Walker_Nav_Menu

例子

<&#63;php wp_nav_menu( array( 'theme_location' => 'header_menu', 'container' => false, 'items_wrap' => '<ul id="topmenu">%3$s</ul>', 'fallback_cb' => 'Bing_menu_null_fallback' ) ); &#63;>

其它

此函数位于:wp-includes/nav-menu-template.php

Statement
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
PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

See all articles

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.