• 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. Please use the pastebin]

Viewing 1 replies (of 1 total)
  • Thread Starter froggyfrog

    (@froggyfrog)

    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.