1

I have no idea why I'm finding this so hard. I've gone through many similar posts here, but none are working.

I am building a menu outside of the loop on my index page - it a standard posts page, not a static page set as home. Basically, I want the current page's menu title (ie: Home) to be echoed onto the page. I will put the same code on single/page/archive/etc. Then I'm using wp_nav_menu to echo out the menu beneath it, like so:

<?php wp_nav_menu( array( 'theme_location' => 'secondary', 'container' => 'div', 'container_class' => 'submenu-container sub-menu clearfix', 'menu_class' => 'ipf-sectionmenu' ) ); ?>  

So reasonably straightforward.... except for just echoing out the menu title. Temporarily, I'm just echoing out the first item in the menu, which is almost always Home, so that works. Like so:

<?php
    $top = wp_get_nav_menu_items('Main');
    $name = $top[0]->post_title;
?>
<h3 id="left-subheader-mobile" class="purple-header"><?php echo $name; ?></h3>

What am I missing?

2

When you filter wp_nav_menu, you get the nav menu arguments as second parameter $args. That's an object (a stdClass), set by WordPress while figuring out which menu to use, with an entry named menu. This is the whole nav menu object. Its title is in the entry name.

Here is a trick: you can pass custom arguments to wp_nav_menu().

Let's say you are calling wp_nav_menu() like this:

wp_nav_menu(
    [
        'theme_location'  => 'secondary',
        'container'       => 'div',
        'container_class' => 'submenu-container sub-menu clearfix',
        'menu_class'      => 'ipf-sectionmenu',
        'heading'         => '<h3 id="left-subheader-mobile" class="purple-header">%s</h3>',
    ]
);

Now you have the HTML for the heading in the arguments, and it is available together with the actual menu title on the hook wp_nav_menu. All you have to do is bringing it together.

Dead simple, just add this to your functions.php:

add_filter( 'wp_nav_menu', function( $nav_menu, $args ) {

    if ( empty ( $args->heading ) )
        return $nav_menu;

    $title   = esc_html( $args->menu->name );
    $heading = sprintf( $args->heading, $title );

    return $heading . $nav_menu;
}, 10, 2 );
  • Thanks for this! I'm really not sure if it is doing what I asked (which probably wasn't clear), or if I'm just too obtuse to understand what's going on (likely). I'm looking to get the title of the menu item that is attached to the page that I'm currently viewing, whatever that may be, so that I may then build the menu beneath that top level item. For instance, my page may be: Events Nearby and it may be accessible from a custom menu with that item named 'Events'. I want to spit out 'Events' then display all the items in that custom menu under it. So how do I do that with your function? – oliverh72 Jul 23 '15 at 22:32
  • @oliverh72 Oh, then I misunderstood you. One post object can occur multiple times in one menu, each time with a different title. There is no direct relation between the object and the title(s). In this case you could filter wp_nav_menu_objects and search through all items, or create a custom walker. – fuxia Jul 23 '15 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.