wp_nav_menu_items not pulling sub-menu
-
I’m trying to build a nav menu using
wp_nav_list_items
, and I’m having trouble getting the submenu items to appear. It is working, to a point, but some of the submenu items appear, and some don’t, and I can’t figure out why.Also, I’m only able to access one layer deep in the menu. For example, some of the submenu items also have submenu items, but those don’t appear at all.
Here’s the code I’m working with, from my header.php file:
<section class="nav"> <?php $menu_name = 'main'; $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' ) ); ?> <ul class="nav__list"> <?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="nav__list-item"> <a href="<?php echo $link; ?>" class="nav__list-link"> <?php echo $title; ?> </a> <?php endif; ?> <?php if ( $parent_id == $item->menu_item_parent ): ?> <?php if ( !$submenu ): $submenu = true; ?> <ul class="sub-menu nav__list-content"> <div class="nav__container"> <div class="row"> <div class="nav__container-page"> </div> <div class="nav__list-children"> <?php endif; ?> <li class="nav__children"> <a href="<?php echo $link; ?>" class="nav__children-head"><?php echo $title; ?></a> </li> <?php if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ): ?> </div> </div> </div> </ul> <?php $submenu = false; endif; ?> <?php endif; ?> <?php $count++; endforeach; ?> </ul> </section>
The page I need help with: [log in to see the link]
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘wp_nav_menu_items not pulling sub-menu’ is closed to new replies.