Alternative to wp_get_nav_menu_items?

I have tried out a technique to implement the wp_get_nav_menu_items, however, a
child on the level 1 is being nested inappropriately on the menu.

Kindly suggest an alternative of how this issue can be handled.

<?php
    $args = array (
        'order'                  => 'ASC',
        'orderby'                => 'menu_order',
        'post_type'              => 'nav_menu_item',
        'post_status'            => 'publish',
        'output'                 => ARRAY_A,
        'output_key'             => 'menu_order',
        'nopaging'               => true,
        'update_post_term_cache' => false );

    $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, $args );?>

  <nav class="sf-iconMenu">
    <ul id="menu-topnav" class="sf-menu_mod">

    <?php 
      foreach ( $menuitems as $item ) :
        // get page id from using menu item object id
        $id = get_post_meta( $item->ID, '_menu_item_object_id', true );
        $page = get_page( $id );
        $title = $page->post_title;
        $link = get_page_link( $id );
        // item does not have a parent so menu_item_parent equals 0 (false)?>

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

           // save this id for later comparison with sub-menu items
           $parent_id = $item->ID;

           switch (strtolower($title)){
           case 'home': $icon='home.png';
                        break;
           case 'about us': $icon='about_us.png';
                            break;
           case 'services': $icon='services.png';
                            break;
           case 'products': $icon='products.png';
                            break;
           case 'energy solutions': $icon='energy_solns.png';
                                    break;
           case 'contact us': $icon='contact_us.png';
                              break;
           default: $icon='';
           }
           ?>      
    <li class="item">
        <a href="<?php echo $link; ?>" class="title">
            <img src="<?php echo bloginfo( 'template_url' ).'/images/icons/'.$icon; ?>" />
            <span><?php echo $title; ?></span>
        </a>
        <?php endif;?>

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

            <?php if ( !$submenu ): $submenu = true; ?>
            <ul class="sub-menu" par_id="<?php echo $parent_id; ?>" count="<?php echo $count; ?>">
            <?php endif; ?>

                <?php 
                // Retrieve second level menu through appropriate query
                $args = array (
                            'sort_order'   => 'ASC',
                            'sort_column'  => 'menu_order',
                            'child_of'     =>  $parent_id,
                            'parent'       => -1,   
                            'post_type'    => 'page',
                            'post_status'  => 'publish'
                        );
                $pages_under = get_pages($args); ?>
                <li class="item">
                    <a href="<?php echo $link; ?>" class="title"><?php echo $title; ?></a>
                    <?php if ( count($pages_under)>0 ): ?>
                        <ul class="sub-menu2">

                            <?php foreach($pages_under as $page):
                                     $id    = $page->ID;     
                                     $title = $page->post_title;
                                     $link  = get_page_link($id); ?>
                            <li class="item">
                                <a href="< ?php echo $link; ?>" class="title">< ?php echo $title; ?></a>
                            </li>
                            <?php endforeach; ?>   
                        </ul>
                    <?php endif; ?>
                </li>
                <?php if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ): ?>
            </ul>
            <?php $submenu = false; // endif; ?>

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

<?php $count++; endforeach; ?>
  </ul>
</nav>

Related posts