WordPress.org

Ready to get started?Download WordPress

Forums

Display wp_nav_menu Subnav ONLY if its Related Parent/Child is Active (4 posts)

  1. chrisfullman
    Member
    Posted 3 years ago #

    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!

  2. chrisfullman
    Member
    Posted 3 years ago #

    Anyone? Here's a better look at what I'm trying to accomplish: http://cl.ly/3c6da46fa529b6a3799d

    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?

  3. Terrycy
    Member
    Posted 3 years ago #

    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:

    <?php
    if ($post->post_parent)	{
    	$ancestors=get_post_ancestors($post->ID);
    	$root=count($ancestors)-1;
    	$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:

    <?php
    print "<pre>";
    print_r($ancestors);
    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:

    <?php
        # 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:

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

    Hope this helps!
    Tez

  4. cannon303
    Member
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic