Search code examples
jquerywordpressdrop-down-menuparent-childsubmenu

Keep Sub-Menu Open when Viewing Child Pages in Wordpress?


I have a small jQuery menu setup on my site. When you click on a parent link it provides a smooth scrolling effect to display the sub pages.

I'm trying to find out how to keep the menu open whenever you're viewing a subpage? Right now if you click Parent -> Subpage the new page will load with the whole menu closed. I want to keep the parent submenu open for all its child pages(I'm using Wordpress if that matters). Would appreciate any help! Happy to clarify myself if need be.


Solution

  • I've had this problem a while ago, but I didn't use jquery. I think you can use this on you're site. The idea is simple: show a div if you're on a subpage.

    <?php
    global $wp_query;
    
    if( empty($wp_query->post->post_parent) ) {
    $parent = $wp_query->post->ID;
    } else {
    $parent = $wp_query->post->post_parent;
    } ?>
    <?php if(wp_list_pages("title_li=&child_of=$parent&echo=0" )): ?>
    <div id="show">
    <ul id="menu" class="black">
    <li><a href="http://www.myhomepage.com">Home</a></li>
    <?php wp_list_pages("title_li=&child_of=$parent" ); ?>
    </ul>
    </div>
    
    
    <?php endif; ?>
    

    Basically this script checks if you are on a page or on a sub-page. If you are on a sub-page it will show the div with id="show"(the list of all subpages who have the same parent as the supage you're currently on).