1

I've got a problem with a piece of code from a previous developer's website. He appears to have created a custom nav menu using the code from these sources:

source source2

Now the code works fine with WP's menu system, that is until you try to add a third level sub-menu: e.g:

Destinations (top) > Europe > France

Adding anything below Europe will result in this:

enter image description here

So anything below or the child of Europe disappears and it's sibling "Asia" pops out of it's parent (destination) into the top level. I've looked for other sources and answers to this question but I've found none. I've pasted the code I'm using at the moment and it seems (I've checked a few times) nearly identical to the above source, I'm missing something here, could anyone help?

    <div class='navigation'>
      <?php
            $menu_name = 'header_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'      ) );
                              ?>

        <nav>
            <ul class="main-nav nav nav-justified">
        <?php
            $count = 0;
            $submenu = false;

            foreach( (array) $menuitems as $item ):
                // get page id from using menu item object id
                    $id = get_post_meta( $item->ID, '_menu_item_object_id', true );
                // set up a page object to retrieve page data
                    $page = get_page( $id );
                    $link = get_page_link( $id );

                // 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 $item->title; ?>
                            </a>
                            <?php endif; ?>

                            <?php if ( $parent_id == $item->menu_item_parent ): ?>

                                <?php if ( !$submenu ): $submenu = true; ?>
                                    <ul class="sub-menu">
                                        <div class='row'>

                                <?php endif; ?>


                                <?php // Sub menu ?>
                                    <div class='col-md-12'>
                                        <li class="item">
                                            <a href="<?php echo $link; ?>"class="title">
                                                        <!--<div class='nav-item-featured' style='background-image:url(<?php echo $url; ?>);'></div>-->
                                                        <?php //$url = wp_get_attachment_url( get_post_thumbnail_id($id) ); ?>
                                                        <div class='title'><strong><?php echo $page->post_title; ?></strong></div>
                                             </a>
                                         </li>

                                    </div>


                                        <?php if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ): ?>
                                        </div>
                                        </ul>
                                        <?php $submenu = false; endif; ?>

                                    <?php endif; ?>

                                <?php if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id ): ?>
                                </li>                           
                                <?php $submenu = false; endif; ?>

                            <?php $count++; endforeach; ?>

                            </ul>
                            </nav>
                        </div>
  • 1
    Menus architecture is totally not meant to be used in this way. I would strongly advise to scrap this and do proper wp_nav_menu() implementation instead. – Rarst Dec 30 '14 at 15:14
  • Thanks, I had a feeling this code seemed odd considering how menus are normally less troublesome. I'll use wp_nav_menu instead then. – steveBeeSmart Jan 2 '15 at 13:21
  • @steveBeeSmart I wrote that code, if you can tell me how to customise the html that wp_nav_menu() is producing I'll take down that code and burn it :) – hitautodestruct Mar 31 '15 at 13:21
  • @Rarst wp_nav_menu() is very limited to what it can do, even if you extend Walker_Nav_Menu(). The above will be less code and more efficient for customization. – Ricky Boyce Jun 1 '15 at 6:33
  • @RickyB I disagree, Walkers can be unfriendly to code, but they are flexible and can support very elaborate solutions. Snippet in question is just disorganized tag soup. – Rarst Jun 1 '15 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.