Snippets

Rik Kendell Section submenu

You are viewing an old version of this snippet. View the current version.
Revised by Rik Kendell c4bcf73
<?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 ) ) : ?>
    
    <nav class="submenu">
        <ul class="nav">
            
            <?php // Output menu items
            foreach( $menu_items as $menu_item ) :
                echo '<li><a href="' . $menu_item->url . '">' . $menu_item->title . '</a></li>';		   		
            endforeach; ?>
            
        </ul>
    </nav>

<?php endif; ?>	
HTTPS SSH

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