• 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)
  • Moderator bcworkz

    (@bcworkz)

    Why are you trying to build your own menu HTML? What is objectionable about the output of wp_nav_menu()? The problem with getting items with wp_get_nav_menu_items() is the array organization is not hierarchical. The nature of your code simply cannot handle more than one sub-menu level. A different approach is required. The logic required to rebuild a hierarchical structure from what we get hurts my brain!

    The wp_nav_menu() function has a number of filters you can use to alter what it outputs if you find anything objectionable. If all else fails, you can define your own walker class to be used where you have total control over what is output. There is no reason I can imagine to not use wp_nav_menu().

Viewing 1 replies (of 1 total)
  • The topic ‘wp_nav_menu_items not pulling sub-menu’ is closed to new replies.