wp_nav_menu_items
Allows you to change the generated HTML of menu items.
Approximately the following content is passed through the filter:
<li id="menu-item-265" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-265"> <a href="http://wp-test.ru/post-99">Feedback</a> </li> <li id="menu-item-266" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-266"> <a href="http://wp-test.ru/post-98">All articles</a> </li> <li id="menu-item-267" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-267"> <a href="http://wp-test.ru/post-97">Sitemap</a> </li>
Usage
add_filter( 'wp_nav_menu_items', 'wp_kama_nav_menu_items_filter', 10, 2 );
/**
* Function for `wp_nav_menu_items` filter-hook.
*
* @param string $items The HTML list content for the menu items.
* @param stdClass $args An object containing wp_nav_menu() arguments.
*
* @return string
*/
function wp_kama_nav_menu_items_filter( $items, $args ){
// filter...
return $items;
}
- $items(string)
- The HTML of the menu items.
- $args(stdClass)
- The object containing parameters passed to wp_nav_menu().
Examples
#1 Add a search form to the menu
Add an item containing a search form to the menu assigned to the header-menu location.
add_filter( 'wp_nav_menu_items', 'change_nav_menu_items', 10, 2 );
function change_nav_menu_items( $items, $args ) {
if ( 'header-menu' == $args->theme_location ) {
$items .= '<li>' . get_search_form( false ) . '</li>';
}
return $items;
}Changelog
| Since 3.0.0 | Introduced. |
Where the hook is called
wp_nav_menu_items
wp-includes/nav-menu-template.php 272
$items = apply_filters( 'wp_nav_menu_items', $items, $args );