Support » Fixing WordPress » Display wp_nav_menu Subnav ONLY if its Related Parent/Child is Active

  • Hey peoples!

    I’m stumped trying to output the subnav (via wp_nav_menu) only if its related parent/child link is active.

    I’ve seen a few of the custom walkers on this support post and tried using them, but the examples either only show the current menu and it’s subnav, or remove them completely.

    So, what I’m trying to achieve is, if I have a menu of [Home, Projects, Gallery, Contact] and only projects and gallery have sub pages set in the menu editor, if I’m on Home or Contact, subnav is never visible (obviously), but when you click on Projects, you’d ONLY see the Project’s subnav (and never see the Gallery subnav).

    Any ideas?

    Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Anyone? Here’s a better look at what I’m trying to accomplish:

    I have 4 top-level nav points, and each of those nav points have their own subnav. So in the WordPress menu editor, I have the following structure:

    • Who We Are
      • Our People
      • Our Story
      • Our Goal
      • Our Progress
    • Projects
      • Current
      • Existing
      • Upcoming
    • Join the Movement
      • Find an Event
      • Create a Fundraiser
      • Download a Media Kit
      • Create a Badge

    Again, I’m only looking to show the subnav of the current top-level link ONLY if the top-level link is active or one of its child links is active.

    Any ideas on how to do this with a custom walker?

    Hello Chris,
    I’m searching for a more advanced way of coding a menu, but in the process of creating/looking, I have come up with a nifty little snipet if you want to give it a go!

    In your chosen template, open page.php and pop the following in:

    if ($post->post_parent)	{
    	$parent = $ancestors[0];
    } else {
    	$parent = $post->ID;
    $page_data = get_page($page_id);

    This allows WP to identify where it is in terms of navigation, if you want to see the actual ID’s of the page just pop this in:

    print "<pre>";
    print "</pre>";
    <br/> Parent ID:<?php echo $parent?><br/>Page ID:<?php echo $page_id?>

    The code above does nothing but prints out the information you may need.

    Where you want to place your menu, try this:

        # must set child of to be dynamic
        wp_nav_menu( array( 'depth' => '1', 'theme_location' => 'primary', 'child_of' => '$PARENT') ); ?>

    The tag: $parent is the ID of the parent page, this code is basically telling wordpress to only show the menu of it’s parent.
    Failing that, I have also done this:

      $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
      $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
      if ($children) { ?>
      <?php echo $children; ?>
      <?php } ?>

    Hope this helps!

    I too am looking for this solution and the above code doesn’t work.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display wp_nav_menu Subnav ONLY if its Related Parent/Child is Active’ is closed to new replies.