0
votes

I need help with combining these two codes:

this:

<?php wp_nav_menu(
                array(
                    'theme_location'  => 'primary',
                    'container_class' => 'collapse navbar-collapse',
                    'container_id'    => 'navbarNavDropdown',
                    'menu_class'      => 'navbar-nav',
                    'fallback_cb'     => '',
                    'menu_id'         => 'main-menu',
                    'walker'          => new rorentrep_WP_Bootstrap_Navwalker(),
                )
            );
?>

and this (from https://www.minddevelopmentanddesign.com/blog/showing-current-pages-parents-sub-menu-items-custom-nav-menu-wordpress/):

<?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>';
             }
?>

More precisely, I want the links that the second code echoes to appear as the first code.

The structure of my pages are like this:

Business
 - Sub page a
 - Sub page b
 - Sub page c

Private
 - Sub page x
 - Sub page y
 - Sub page z

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.

As multiple pages can be added, I do not want to target specific page id's.

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).

1

1 Answers

0
votes

In your walker class, you can set the behavior for each level. Then check the depth in the element method: start_el

class Footer extends \Walker_Nav_Menu
{
    function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
    {
      if ($depth == 0) {
        $output .= '
        <li class="list-inline-item">
          <a href="' . $item->url . '" title="' . $item->title . '">
            ' .  $item->title  . '
          </a>
        </li>';
      }
    }
    function start_lvl( &$output, $depth = 0, $args = array() ) {
      $output.= '';
    }
    function end_lvl( &$output, $depth = 0, $args = array() ) {
      $output.= '';
    }
}