admin管理员组

文章数量:1313121

I know I can edit the items within the wp_nav_menu by declaring a custom walker, but I want to add some code just inside the container, which isn't handled by a walker.

It's in nav-menu-menu-template.php, so I can get the desired effect by adding my code after line 329...

$nav_menu .= "<div class='topper'>
                  <a href='".get_home_url()."' class='intranet'>H</a>
              </div>";

...but of course this will disappear on an update. What hook do I need to achieve the same?

I know I can edit the items within the wp_nav_menu by declaring a custom walker, but I want to add some code just inside the container, which isn't handled by a walker.

It's in nav-menu-menu-template.php, so I can get the desired effect by adding my code after line 329...

$nav_menu .= "<div class='topper'>
                  <a href='".get_home_url()."' class='intranet'>H</a>
              </div>";

...but of course this will disappear on an update. What hook do I need to achieve the same?

Share Improve this question edited Sep 23, 2015 at 12:57 SinisterBeard asked Sep 23, 2015 at 12:01 SinisterBeardSinisterBeard 1,2173 gold badges14 silver badges34 bronze badges 2
  • 1 After the container, or after the nav menu but inside the container? – TheDeadMedic Commented Sep 23, 2015 at 12:15
  • @TheDeadMedic After the nav but inside the container. – SinisterBeard Commented Sep 23, 2015 at 12:56
Add a comment  | 

2 Answers 2

Reset to default 1

Solved it by setting container to false and then wrapping it manually.

<nav class="main_navigation">
    <div class="topper">
        <a href="<?=get_home_url()?>" class="intranet">H</a>
    </div>
    <?php wp_nav_menu(array('container'=>false) ?>
</nav>

You can also hook the items of the menu:

wp-includes/nav-menu-template.php

/**
 * Filters the HTML list content for navigation menus.
 *
 * @since 3.0.0
 *
 * @see wp_nav_menu()
 *
 * @param string   $items The HTML list content for the menu items.
 * @param stdClass $args  An object containing wp_nav_menu() arguments.
 */
$items = apply_filters( 'wp_nav_menu_items', $items, $args );

Example of adding a custom item on the begin of the nav:

function add_custom_item( $items, $args ) {
    $custom_item = "<a href='".get_home_url()."' class='intranet'>H</a>";
    return $custom_item . $items;
}
add_filter( 'wp_nav_menu_items', 'add_custom_item', 10, 2 );

本文标签: functionsHow do I hook into the container of wpnavmenu