Snippets

Rik Kendell Section submenu

Created by Rik Kendell last modified
<nav class="submenu">
	<div class="wrapper">
	    <?php // SUBMENU - active page's children (or siblings if on child page)

	    // If no ancestors, use current page. Else use last ancestor
	    $section_id = empty( $post->ancestors ) ? $post->ID : end( $post->ancestors );

	    // Get theme menu locations
	    $locations = get_nav_menu_locations();

	    // Navigation location/name
	    $menu = wp_get_nav_menu_object( $locations[ 'main-menu' ] ); // ie 'main-menu' is our nav menu's name

	    // Submenu items of identified section
	    $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'post_parent' => $section_id ) );
		
	    if( !empty( $menu_items ) ) :

	    	// Hamburger toggle ?>
	    	<button class="hamburger hamburger--spin toggle-submenu" type="button" aria-label="Menu" aria-controls="navigation">
		  		<span class="hamburger-box">
		    		<span class="hamburger-inner"></span>
		  		</span>
			</button>
	    	
	    	<?php // Output menu items ?>
		    <ul class="section-submenu container nav">
			    <?php foreach( $menu_items as $menu_item ) : ?>
			   		<li><a href="<?php echo $menu_item->url; ?>"><?php echo $menu_item->title; ?></a></li>
			    <?php endforeach; ?>
	    	</ul>	   		

		<?php endif; ?>	
	</div>
</nav>

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.