zartgesotten
9/20/2017 - 5:50 AM

Display current Menu Parents children / siblings of current page

Source: https://www.minddevelopmentanddesign.com/blog/showing-current-pages-parents-sub-menu-items-custom-nav-menu-wordpress/

But how do you achieve the same thing if you’re using a custom menu, from the Appearance -> Menus area in the site administration, and your page child-parent relationships are handled separately? There’s a little bit more to it. This snippet will grab the top level page’s submenu items and display them in an unordered list, useful for generating submenus in some scenarios.

<?php
$section_id = empty( $post->ancestors ) ? $post->ID : end( $post->ancestors );
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ 'primary' ] ); // 'primary' is our nav menu's name
$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'post_parent' => $section_id ) );
if( !empty( $menu_items ) ) {
echo '<ul class="section-submenu">';
foreach( $menu_items as $menu_item ) {
echo '<li><a href="' . $menu_item->url . '">' . $menu_item->title . '</a></li>';
}
echo '</ul>';
}
?>