wpseek.com
A WordPress-centric search engine for devs and theme authors



block_core_navigation_sort_menu_items_by_parent_id › WordPress Function

Sincen/a
Deprecatedn/a
block_core_navigation_sort_menu_items_by_parent_id ( $menu_items )
Parameters:
  • (array) $menu_items Menu items to sort.
    Required: Yes
Returns:
  • (array) An array keyed by the id of the parent menu where each element is an array of menu items that belong to that parent.
Defined at:
Codex:

Sorts a standard array of menu items into a nested structure keyed by the id of the parent menu.



Source

function block_core_navigation_sort_menu_items_by_parent_id( $menu_items ) {
		$sorted_menu_items = array();
		foreach ( (array) $menu_items as $menu_item ) {
			$sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
		}
		unset( $menu_items, $menu_item );

		$menu_items_by_parent_id = array();
		foreach ( $sorted_menu_items as $menu_item ) {
			$menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item;
		}

		return $menu_items_by_parent_id;
	}

	/**
	 * Turns menu item data into a nested array of parsed blocks
	 *
	 * @param array $menu_items               An array of menu items that represent
	 *                                        an individual level of a menu.
	 * @param array $menu_items_by_parent_id  An array keyed by the id of the
	 *                                        parent menu where each element is an
	 *                                        array of menu items that belong to
	 *                                        that parent.
	 * @return array An array of parsed block data.
	 */