Title: show/hide submenu
Last modified: August 19, 2016

---

# show/hide submenu

 *  [froggyfrog](https://wordpress.org/support/users/froggyfrog/)
 * (@froggyfrog)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/showhide-submenu/)
 * Hi there,
 * I have created my own theme for wordpress and all works. However, I would like
   my subnav to only show once the parent has been selected.
 * I think I need some kind of jquery to do this but not too sure how it all works.
 * Basically I have a mainnav and for some of the mainnav options there is a subnav.
   Once a mainnav item is selected the a is given an id of active.
 * Any help would be great thank you,
 * Here is my code:-
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome).
   Please use the [pastebin](http://wordpress.pastebin.com)]_

Viewing 1 replies (of 1 total)

 *  Thread Starter [froggyfrog](https://wordpress.org/support/users/froggyfrog/)
 * (@froggyfrog)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/showhide-submenu/#post-1790874)
 * The code:-
 *     ```
       <?php
       // Get top level parent for page
       if ($post->post_parent)
       {
       	$ancestors=get_post_ancestors($post->ID);
       	$root=count($ancestors)-1;
       	$topParent = $ancestors[$root];
       }
       else
       {
       	$topParent = $post->ID;
       }
   
       // This function gets the main nav items
       $args = array
       (
       	'sort_column' => 'ID',
       	'parent' => 0,
       );
       $mainNavItems = get_pages($args);
       ?>
       <script type="text/javascript">
       // For: http://www.webdeveloper.com/forum/showthread.php?t=239126
   
       function toggleID(IDS) {
         var sel = document.getElementById(IDS);
         sel.style.display = (sel.style.display != 'block') ? 'block' : 'none';
       }
       </script>
   
       <body>
       <div id="wrapper">
       	<?php
       	// Get current URL
       	$url = $_SERVER['PHP_SELF'];
       	?>
       	<div class="leftbar">
           	<div class="logo"></div>
               <ul class="mainnav">
                   <?php
       			foreach($mainNavItems as $link)
       			{
       				// For each main nav item, check against current page ID.  If matches attach 'active' to that list item
       				$active = '';
       				if ($link->ID == $topParent) {$active = ' id="active"';}
   
       				// Get all subpages of current main nav item
       				$args = array
       				(
       					'sort_column' => 'menu_order',
       					'child_of' => $link->ID,
       					'parent' => $link->ID
       				);
       				$subNavItems = get_pages($args);
       			?>
       			<li><a class="slide" <?php echo $active; ?> href="<?php echo get_page_link($link->ID); ?>"><?php echo $link->post_title; ?></a>
                   <?php
       			// This line checks if there are any subpages for this main nav item
                   if (count($subNavItems))
       			{
       			?>
                   <ul class="subnav" id="sub1" style="display:none">
       				<?php
                       $i=0;
                       foreach($subNavItems as $subpage)
                       {
                       // This just adds a class to the first subnav item
                       $first = '';
                       if ($i == 0) {$first = ' class="first"';}
   
                       // Get all subpages of current nav item
                       $args = array
                       (
                       'sort_column' => 'menu_order',
                       'child_of' => $subpage->ID,
                       'parent' => $subpage->ID
                       );
   
                       ?>
                       <li<?php echo $first; ?>><a href="<?php echo get_page_link($subpage->ID); ?>"<?php echo $class; ?>><?php echo $subpage->post_title; ?></a>
                   <?php } ?>
                   </ul>
                   <?php } ?>
                   </li>
                   <?php }?>
       		</ul>
           </div>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘show/hide submenu’ is closed to new replies.

## Tags

 * [Subnav](https://wordpress.org/support/topic-tag/subnav/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [froggyfrog](https://wordpress.org/support/users/froggyfrog/)
 * Last activity: [15 years, 5 months ago](https://wordpress.org/support/topic/showhide-submenu/#post-1790874)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
