WP_NAV_MENU()函数参数默认值:
= array(
'theme_location' => ,
'menu' => ,
'container' => 'div',
'container_class' => 'menu-{menu slug}-container',
'container_id' => ,
'menu_class' => 'menu',
'menu_id' => ,
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => ,
'after' => ,
'link_before' => ,
'link_after' => ,
'items_wrap' => '',
'depth' => 0,
'walker' => );
?>
( $defaults ); ?>
参数
-
$theme_location
-
主题位置,必须用 register_nav_menu()注册菜单以供用户选择
-
Default:
None:默认值为空
-
$menu
-
用来匹配id slug name属性是必要的
-
Default:
None 默认值空
-
$container
-
确定菜单是被什么标签包含,可以为div标签或者nav标签。没有标签包含是使用false
e.g. container => false
-
Default: div(默认为div)标签
-
$container_class
-
容器应用的类(即标签中的class属性)
-
Default: menu-{menu slug}-container
-
$container_id
-
容器应用的ID(标签中的ID)
-
Default:
None
-
$menu_class
-
CSS类使用div元素形成默认的菜单,或ul元素当一个自定义菜单中配置管理界面e
-
Default: menu
-
$menu_id
-
应用于ul元素构成的菜单的
ID,
-
Default: menu slug, incremented
-
$echo
-
是否要呼应菜单或返回它。返回菜单使用' 0 '
-
Default: true
-
$fallback_cb
-
(
string) (
optional) If the menu doesn't exist, the fallback function to use. Set to false for no fallback.
Note: passes $args to the custom function.
-
-
Default: wp_page_menu
-
$before
-
(
string) (
optional) Output text before the of the link
-
Default:
None
-
$after
-
(
string) (
optional) Output text after the of the link
-
Default:
None
-
$link_before
-
(
string) (
optional) Output text before the link text
-
Default:
None
-
$link_after
-
(
string) (
optional) Output text after the link text
-
Default:
None
-
$items_wrap
-
(
string) (
optional) Whatever to wrap the items with an ul, and how to wrap them with
-
Default:
None
-
$depth
-
(
integer) (
optional) how many levels of the hierarchy are to be included where 0 means all
-
Default: 0
-
$walker
-
(
object) (
optional) Custom walker object to use (Note: You must pass an actual object to use, not a string)
-
Default:
new Walker_Nav_Menu
Examples
Default example
Targeting a specific Menu
'Project Nav' )); ?>
Used in the Twenty Ten theme
'menu-header', 'theme_location' => 'primary' ) ); ?>
Removing the Navigation Container
In order to remove navigation container, theme location specified in functions.php and used among arguments in function wp_nav_menu ( eg. 'theme_location' => 'primary-menu' ) must have a menu assigned to it in administration! Othervise argument 'container' => 'false' is ignored.
OR
'' ) ); ?>
Removing the ul wrap
This example will remove the ul around the list items.
'%3$s' ) ); ?>
Adding a Word at the Beginning of the Menu
This example will allow you to add the word of your choice to the beginning of your menu as a list item. In this example, the word "Menu:" is added at the beginning. You may want to set an id on the list item ("item-id" in this example) so that you can use CSS to style it.
'primary', 'items_wrap' => '
' ) ); ?>
Adding Conditional Classes to Menu Items
This example would let you add a custom class to a menu item based on the condition you specify. Don't forget to change the condition.
title == "Blog"){ //Notice you can change the conditional from is_single() and $item->title
$classes[] = "special-class";
}
return $classes;
}
?>
I was trying to customize the look of a specific menu item: Blog on single post pages. After rethinking the code above, it is much simpler to use the body class .single if you can. In my case it works. But nonetheless, the above code is really handy.
Using a Custom Walker Function
For deeper conditional classes, you'll need to use a custom walker function (created in the 'walker' => new Your_Walker_Function argument).
The easiest way to build a new walker function is to copy the default class (Walker_Nav_Menu
) from \wp-includes\nav-menu-template.php and simply customize what you need.
You can find an example that adds menu-depth and even/odd classes to both ul and li elements here.
Different menus for logged-in users
This example would cause a menu to show for logged-in users and a different menu for users not logged-in.
'logged-in-menu' ) );
} else {
wp_nav_menu( array( 'theme_location' => 'logged-out-menu' ) );
}
?>
How to add a parent class for menu item
Soemtimes you may need a "parent stat" class for all menu item which has sub menus.
add_filter('wp_nav_menu_objects', function ($items) {
$hasSub = function ($menu_item_id, &$items) {
foreach ($items as $item) {
if ($item->menu_item_parent && $item->menu_item_parent==$menu_item_id) {
return true;
}
}
return false;
};
foreach ($items as &$item) {
if ($hasSub($item->ID, &$items)) {
$item->classes[] = 'menu-parent-item'; // all elements of field "classes" of a menu item get join together and render to class attribute of
element in HTML
}
}
return $items;
});
Menu Item CSS Classes
The following classes are applied to menu items, i.e. to the HTML tags, generated by wp_nav_menu():
All Menu Items
- .menu-item
This class is added to every menu item.
- .menu-item-object-{object}
This class is added to every menu item, where {object} is either a post type or a taxonomy.
- .menu-item-object-category
This class is added to menu items that correspond to a category.
- .menu-item-object-tag
This class is added to menu items that correspond to a tag.
- .menu-item-object-page
This class is added to menu items that correspond to static pages.
- .menu-item-object-{custom}
This class is added to menu items that correspond to a custom post type or a custom taxonomy.
- .menu-item-type-{type}
This class is added to every menu item, where {type} is either "post_type" or "taxonomy".
- .menu-item-type-post_type
This class is added to menu items that correspond to post types: i.e. static pages or custom post types.
- .menu-item-type-taxonomy
This class is added to menu items that correspond to taxonomies: i.e. categories, tags, or custom taxonomies.
Current-Page Menu Items
- .current-menu-item
This class is added to menu items that correspond to the currently rendered page.
Current-Page Parent Menu Items
- .current-menu-parent
This class is added to menu items that correspond to the hierarchical parent of the currently rendered page.
- .current-{object}-parent
This class is added to menu items that correspond to the hierachical parent of the currently rendered object, where {object} corresponds to the the value used for .menu-item-object-{object}.
- .current-{type}-parent
This class is added to menu items that correspond to the hierachical parent of the currently rendered type, where {type} corresponds to the the value used for .menu-item-type-{type}.
Current-Page Ancestor Menu Items
- .current-menu-ancestor
This class is added to menu items that correspond to a hierarchical ancestor of the currently rendered page.
- .current-{object}-ancestor
This class is added to menu items that correspond to a hierachical ancestor of the currently rendered object, where {object} corresponds to the the value used for .menu-item-object-{object}.
- .current-{type}-ancestor
This class is added to menu items that correspond to a hierachical ancestor of the currently rendered type, where {type} corresponds to the the value used for .menu-item-type-{type}.
Site Front Page Menu Items
- .menu-item-home
This class is added to menu items that correspond to the site front page.
Backward Compatibility with wp_page_menu()
The following classes are added to maintain backward compatibility with the wp_page_menu() function output:
- .page_item
This class is added to menu items that correspond to a static page.
- .page-item-$ID
This class is added to menu items that correspond to a static page, where $ID is the static page ID.
- .current_page_item
This class is added to menu items that correspond to the currently rendered static page.
- .current_page_parent
This class is added to menu items that correspond to the hierarchical parent of the currently rendered static page.
- .current_page_ancestor
This class is added to menu items that correspond to a hierarchical ancestor of the currently rendered static page.
Change log
Source file
wp_nav_menu() is located in wp-includes/nav-menu-template.php
.