title: WordPress: only showing the current page’s top-level parent’s sub-menu items from a custom nav menu
body: <p>I need help with combining these two codes:</p>
  
  <p>this:</p>
  
  <pre><code>&lt;?php wp_nav_menu(
                  array(
                      'theme_location'  =&gt; 'primary',
                      'container_class' =&gt; 'collapse navbar-collapse',
                      'container_id'    =&gt; 'navbarNavDropdown',
                      'menu_class'      =&gt; 'navbar-nav',
                      'fallback_cb'     =&gt; '',
                      'menu_id'         =&gt; 'main-menu',
                      'walker'          =&gt; new rorentrep_WP_Bootstrap_Navwalker(),
                  )
              );
  ?&gt;
  </code></pre>
  
  <p>and this (from <a href="https://www.minddevelopmentanddesign.com/blog/showing-current-pages-parents-sub-menu-items-custom-nav-menu-wordpress/" rel="nofollow noreferrer">https://www.minddevelopmentanddesign.com/blog/showing-current-pages-parents-sub-menu-items-custom-nav-menu-wordpress/</a>):</p>
  
  <pre><code>&lt;?php
           $section_id = empty( $post-&gt;ancestors ) ? $post-&gt;ID : end( $post-&gt;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-&gt;term_id, array( 'post_parent' =&gt; $section_id ) );
  
               if( !empty( $menu_items ) ) {
                   echo '&lt;ul class="section-submenu"&gt;';
                   foreach( $menu_items as $menu_item ) {
                   echo '&lt;li&gt;&lt;a href="' . $menu_item-&gt;url . '"&gt;' . $menu_item-&gt;title . '&lt;/a&gt;&lt;/li&gt;';
               }
               echo '&lt;/ul&gt;';
               }
  ?&gt;
  </code></pre>
  
  <p>More precisely, I want the links that the second code echoes to appear as the first code.</p>
  
  <p>The structure of my pages are like this:</p>
  
  <pre><code>Business
   - Sub page a
   - Sub page b
   - Sub page c
  
  Private
   - Sub page x
   - Sub page y
   - Sub page z
  </code></pre>
  
  <p>Whenever I visit a child's page of Business I want the menu to only list Sub page a-c, and when I visit a child's page of Private I want the menu to only list Sub page x-z.</p>
  
  <p>As multiple pages can be added, I do not want to target specific page id's.</p>
  
  <p>The main reason for this is for the output code to wrap and nest like wp_nav_menu using walker bootstrap nav (adding current-classes to nav-items). </p>