WordPress Menu Helper Class

This small class will help you in more advanced situations like getting the parent id of the current page, or find the children of  specific nav item.

Let’s say we have a menu location named primary then we can do :

#Example 1:

Get the parent id of current page

MInc_Menu::getMenuParentId( 'primary' );

#Example 2:

Get the menu children of current page

$siblings = MInc_Menu::getNavMenuItemChildren(
   MInc_Menu::getMenuParentId( 'primary' ),
   wp_get_nav_menu_items( 'primary-menu' )
);

 

Class 

<?php

class MInc_Menu {

   /**
    * @param $parent_item_id
    * @param $menu_items
    *
    * @return array|mixed
    */
   public static function _checkForParent( $parent_item_id, $menu_items ) {
      $parent_post_id = wp_filter_object_list( $menu_items, [ 'ID' => $parent_item_id ], 'and', 'object_id' );
      $parent_item_id = wp_filter_object_list( $menu_items, [ 'ID' => $parent_item_id ], 'and', 'menu_item_parent' );
      $parent_item_id = array_shift( $parent_item_id );
      if ( $parent_item_id == "0" ) {
         $parent_post_id = array_shift( $parent_post_id );

         return $parent_post_id;
      } else {
         return self::_checkForParent( $parent_item_id, $menu_items );
      }
   }

   /**
    * @param $menu_name
    *
    * @return array|false|int|mixed|string
    */
   public static function getMenuParentId( $menu_name = null, $post_id = null ) {
      if ( $menu_name === null ) {
         return 'No menu name provided in arguments';
      }
      $menu_slug = $menu_name;
      $locations = get_nav_menu_locations();
      $menu_id   = $locations[ $menu_slug ];
      if ( $post_id === null ) {
         $post_id = get_the_ID();
      }
      $menu_items     = wp_get_nav_menu_items( $menu_id );
      $parent_item_id = wp_filter_object_list( $menu_items, [ 'object_id' => $post_id ], 'and', 'menu_item_parent' );
      $parent_item_id = array_shift( $parent_item_id );

      if ( ! empty( $parent_item_id ) ) {
         return self::_checkForParent( $parent_item_id, $menu_items );
      } else {
         return $post_id;
      }
   }

   /**
    * Returns all child nav_menu_items under a specific parent
    *
    * @param int the parent nav_menu_item ID
    * @param array nav_menu_items
    * @param bool gives all children or direct children only
    *
    * @return array returns filtered array of nav_menu_items
    */
   public static function getNavMenuItemChildren( $parent_id, $nav_menu_items, $depth = true ) {
      $nav_menu_item_list = array();
      foreach ( (array) $nav_menu_items as $nav_menu_item ) {
         if ( $nav_menu_item->menu_item_parent == $parent_id ) {
            $nav_menu_item_list[] = $nav_menu_item;
            if ( $depth ) {
               if ( $children = self::getNavMenuItemChildren( $nav_menu_item->ID, $nav_menu_items ) ) {
                  $nav_menu_item_list = array_merge( $nav_menu_item_list, $children );
               }
            }
         }
      }

      return $nav_menu_item_list;
   }
}