Title: Navigation code
Last modified: August 30, 2016

---

# Navigation code

 *  [poizster](https://wordpress.org/support/users/poizster/)
 * (@poizster)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/navigation-code-1/)
 * Using the following code in my navigation and am trying to get rid of duplicating
   menu items.
 * If a top level menu item is called “services” the on hover on the menu will show“
   services” as well. Can you tell me where this happens in the php and how to correct
   it?
 *     ```
       $my_wp_query = new WP_Query();
        $all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
   
        $top_level = false;
   
        if ( isset( $post->ID ) ) {
        	$current_id = $post->ID;
        	$top_level = true;
        	if ( $parent = array_reverse(get_post_ancestors($post->ID)) ) {
        		$current_id = $parent[0];
        		$top_level = false;
        	}
        } else {
        	$current_id = '';
        }
   
        $menu_name = 'primary';
        $has_children = false;
        $secondary_nav = false;
   
        if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
       		$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
   
       		$menu_items = wp_get_nav_menu_items($menu->term_id);
   
       		$menu_list = '<ul id="menu-' . $menu_name . '" class="nav pull-right">';
   
       		foreach ( (array) $menu_items as $key => $menu_item ) {
       		    $title = $menu_item->title;
       		    $url = $menu_item->url;
       		    $link_class = '';
   
       		    if ( $menu_item->object_id == $current_id ) {
       		    	$link_class .= 'current';
       		    }
   
       			$menu_list .= '<li class="' . $link_class . '"><a class="collapseToggle" data-target="#collapse-l1' . $key . '" href="' . $url . '">' . $title . ' <span class="glyphicon glyphicon-chevron-down visible-xs visible-sm"></span></a>';
       			$menu_list .= '<ul id="collapse-l1' . $key . '" class="panel-collapse level-one">';
       			$menu_list .= '<li class="' . $link_class . '"><a class="indent" href="' . $url . '">' . $title . '</a></li>';
   
       			$children = get_pages( array(
       				'parent' => $menu_item->object_id,
       				'post_type' => 'page',
       				'post_statue' => 'publish'
       			) );
   
       			foreach ( (array) $children as $key => $child ) {
       					$link_class = '';
       					if ( isset( $post->ID ) && $child->ID == $post->ID ) {
       						$link_class .= 'current';
       					}
       					$children = get_pages( array(
       						'parent' => $child->ID,
       						'post_type' => 'page',
       						'post_statue' => 'publish'
       					) );
   
       					if ($children) {
       						$menu_list .= '<li><a class="collapseToggle" data-mobile="1" data-target="#collapse-l2' . $key . '" href="/' . get_page_uri( $child->ID ) . '">' . $child->post_title . ' <span class="glyphicon glyphicon-chevron-down visible-xs visible-sm"></span></a>';
       						$menu_list .= '<ul id="collapse-l2' . $key . '" class="panel-collapse level-two">';
       						$menu_list .= '<li class="visible-xs visible-sm ' . $link_class . '"><a href="/' . get_page_uri( $child->ID ) . '">' . $child->post_title . '</a></li>';
   
       						foreach ( (array) $children as $child ) {
       							$displayInMenus = get_field( "display_in_menus", $child->ID, true );
       							if ( !isset( $displayInMenus ) ||  !$displayInMenus ) {
       								$link_class = '';
       								if ( isset( $post->ID ) && $child->ID == $post->ID ) {
       									$link_class .= 'current';
       								}
       								$menu_list .= '<li><a class="' . $link_class . '" href="/' . get_page_uri( $child->ID ) . '">' . $child->post_title . '</a></li>';
       							}
       						}
       						$menu_list .= '</ul>';
       					} else {
       						$menu_list .= '<li><a class="indent" href="/' . get_page_uri( $child->ID ) . '">' . $child->post_title . '</a>';
       					}
       					$menu_list .= '</li>';
   
       			}
   
       			$menu_list .= '</ul>';
       			$menu_list .= '</li>';
   
       		}
       		$menu_list .= '</ul>';
        } else {
       		$menu_list = '<ul><li>Menu "' . $menu_name . '" not defined.</li></ul>';
        }
   
        $primary_nav = $menu_list;
       ```
   

The topic ‘Navigation code’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [poizster](https://wordpress.org/support/users/poizster/)
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/navigation-code-1/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
