1. 主页
  2. 文档
  3. WordPress函数
  4. wp_nav_menu()

wp_nav_menu()

原文:
https://developer.wordpress.org/reference/functions/wp_nav_menu/

原文:https://developer.wordpress.org/reference/functions/wp_nav_menu/

Parameters #Parameters
$args
(array) (Optional) Array of nav menu arguments.
'menu'
(int|string|WP_Term) Desired menu. Accepts a menu ID, slug, name, or object.
'menu_class'
(string) CSS class to use for the ul element which forms the menu. Default 'menu'.
'menu_id'
(string) The ID that is applied to the ul element which forms the menu. Default is the menu slug, incremented.
'container'
(string) Whether to wrap the ul, and what to wrap it with. Default 'div'.
'container_class'
(string) Class that is applied to the container. Default 'menu-{menu slug}-container'.
'container_id'
(string) The ID that is applied to the container.
'container_aria_label'
(string) The aria-label attribute that is applied to the container when it's a nav element.
'fallback_cb'
(callable|bool) If the menu doesn't exist, a callback function will fire. Default is 'wp_page_menu'. Set to false for no fallback.
'before'
(string) Text before the link markup.
'after'
(string) Text after the link markup.
'link_before'
(string) Text before the link text.
'link_after'
(string) Text after the link text.
'echo'
(bool) Whether to echo the menu or return it. Default true.
'depth'
(int) How many levels of the hierarchy are to be included. 0 means all. Default 0. Default 0.
'walker'
(object) Instance of a custom walker class.
'theme_location'
(string) Theme location to be used. Must be registered with register_nav_menu() in order to be selectable by the user.
'items_wrap'
(string) How the list items should be wrapped. Uses printf() format with numbered placeholders. Default is a ul with an id and class.
'item_spacing'
(string) Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'. Default 'preserve'.

Default value: array()


<?php
$args=array(
'menu' => '', //期望显示的菜单,可以是:menu ID、slug、name或object。


'container' => 'div', //外层容器标签,默认是div(即不添加此条参数),添加了,值为空,则不包括外层容器
'container_class' => '',//外层容器class值
'container_id' => '', //外层容器id值

'menu_class' => 'menu', //ul(即次外层标签)的class值,默认值:'menu'。
'menu_id' => '', //ul节点的id值,不填(空)显示添加的后台菜单名,如增加菜单名为“my-menu”,则id="menu-my-menu-1

'items_wrap' => '
%3$s
', //如何包列表
'item_spacing' => 'preserve',//感觉没啥用
'before' => '', //链接前文本
'after' => '', //链接后文本
'link_before' => '', //链接文本前显示的内容
'link_after' => '',//链接文本后显示的内容
'echo' => true,//是否输出菜单,默认为真
'depth' => 0, //菜单深度,默认0,0相当于不配置,输入几显示几层,一般配置0,没有必要改
'fallback_cb' => 'wp_page_menu', //菜单不存在时,返回默认菜单,设为false则不返回,如wp_page_menu为显示所有页面
'walker' => '' //自定义walker
) 
);
?>

范例:

<?php if ( has_nav_menu( 'mymenu' ) ) {								wp_nav_menu(
array(
'container'      => '',
'items_wrap'     => '%3$s',
'show_toggles'   => true,
'theme_location' => 'expanded',
)
);
}
?>

相关函数:

wp_list_pages

items_wrap参数的使用
参数说明:

%1$s:最外层容器(由参数container控制)的class名,即参数container_class的值
%2$s:次一层容器class名,默认为menu,即参数menu_class的值
%3$s:菜单内容

关于’menu’:
如果后台创建“菜单1”,指派到了’menu-1’。
同时后台配置“菜单2”,没有指定菜单位置,设置如下参数
‘menu’ => ‘菜单2’,

这时显示的菜单是菜单2的内容

这篇文章对您有用吗?

我们要如何帮助您?