Search code examples
phpwordpressmenuwordpress-themingnav

How to display 3 levels of hierarchical menu in WordPress using "wp_get_nav_menu_items"?


I am trying to display 3 levels of hierarchical menu in WordPress using wp_get_nav_menu_items function, but somehow the structure is not displaying in that manner.

This is my code which I have included in header file, but the structure is not proper.

$menu_name = 'primary-menu'; 
     $locations = get_nav_menu_locations(); 
     $menu = wp_get_nav_menu_object($locations[$menu_name]); 
     $menuitems = wp_get_nav_menu_items($menu->term_id, array('order' => 'DESC')); 
     $ParentArray = array(); 
     foreach ($menuitems as $item) { 
       // if (!empty($item->menu_item_parent) && !in_array($item->menu_item_parent, $ParentArray)) { 
             array_push($ParentArray, $item->ID); 
        // } 
     }
      ?>

<nav>
  <ul class="main-nav">
    <?php 
        $count = 0; 
        $submenu = false; 
        foreach ($menuitems as $item): 
            $link = $item->url; 
            $title = $item->title; 
            // item does not have a parent so menu_item_parent equals 0 (false) 
            if (!$item->menu_item_parent): 
                // save this id for later comparison with sub-menu items 
                $parent_id = $item->ID; 
                ?>

    <li class="item">
      <a href="
        <?php echo $link; ?>" class="title">
        <?php echo $title; ?>
      </a>
      <?php endif; ?>
      <?php if (in_array($item->menu_item_parent, $ParentArray)): ?>
      <?php if (!$submenu): $submenu = true; ?>
      <ul class="sub-menu">
        <?php endif; ?>
        <li class="item">
          <a href="
            <?php echo $link; ?>" class="title">
            <?php echo $title; ?>
          </a>
        </li>
        <?php if (!isset($menuitems[$count + 1]) || $menuitems[$count + 1]->menu_item_parent != $parent_id && $submenu): ?>
      </ul>
      <?php 
          $submenu = false; 
          endif; 
          ?>
      <?php endif; ?>
      <?php if (!isset($menuitems[$count + 1]) || $menuitems[$count + 1]->menu_item_parent != $parent_id): ?>
    </li>
    <?php 
        $submenu = false; 
        ?>
    <?php 
        $count++; 
        endforeach; 
        ?>
  </ul>
</nav>

Solution

  • After making changes in above code,this things work for me.

        $menu_name = 'primary-menu'; 
         $locations = get_nav_menu_locations(); 
         $menu = wp_get_nav_menu_object($locations[$menu_name]); 
         $menuitems = wp_get_nav_menu_items($menu->term_id, array('order' => 'DESC')); 
         $ParentArray = array(); 
         foreach ($menuitems as $item) { 
           // if (!empty($item->menu_item_parent) && !in_array($item->menu_item_parent, $ParentArray)) { 
                 array_push($ParentArray, $item->ID); 
            // } 
         }?>
    <nav>
      <ul class="main-nav">
        <?php 
            $count = 0; 
            $submenu = false; 
            foreach ($menuitems as $item): 
                $link = $item->url; 
                $title = $item->title; 
                // item does not have a parent so menu_item_parent equals 0 (false) 
                if (!$item->menu_item_parent): 
                    // save this id for later comparison with sub-menu items 
                    $parent_id = $item->ID;  ?>
    
        <li class="item">
          <a href="
            <?php echo $link; ?>" class="title">
            <?php echo $title; ?>
          </a>
          <?php endif; ?>
          <?php if (in_array($item->menu_item_parent, $ParentArray)): ?>
          <?php if (!$submenu): $submenu = true; ?>
          <ul class="sub-menu">
            <?php endif; ?>
            <li class="item">
              <a href="
                <?php echo $link; ?>" class="title">
                <?php echo $title; ?>
              </a>
            </li>
            <?php if (!isset($item[$count + 1]) || $item[$count + 1]->menu_item_parent != $parent_id && $submenu): ?>
          </ul>
          <?php 
              $submenu = false; 
              endif; 
              ?>
          <?php endif; ?>
          <?php if (!isset($menuitems[$count + 1]) || $menuitems[$count + 1]->menu_item_parent != $parent_id): ?>
        </li>
        <?php 
            $submenu = false; 
            ?>
        <?php 
            $count++; 
            endforeach; 
            ?>
      </ul>
    </nav>