-
Get IDs of WordPress Menu Items
WordPress 3 introduced Custom Menu’s, which is a great feature, but when calling a menu with
wp_nav_menu()it builds the menu with the ID’s relative to the menu itself, and not the actual object ID’s the menu items represent.I needed to get the ID’s of a menu list, here’s how I did it:
function get_ids_of_nav_menu($nav_slug) { // lets get all wordpress menus $terms = get_terms('nav_menu'); // cycle through terms (custom menus) and find $nav_slug $term_id = ''; foreach ($terms as $aterm) { if ($aterm->;slug == $nav_slug || $aterm->name == $nav_slug) $term_id = $aterm->term_id; } // get all objects in found menu $items = get_objects_in_term( $term_id , 'nav_menu' ); // get the custom navigations ID's of pages $menuids = array(); foreach ($items as $uselss => $menu_id) { $realitem = get_post($menu_id); $object_id = get_post_meta( $realitem->ID, '_menu_item_object_id', true ); // get the specified menu order, use as key for sorting after $menuids[$realitem->menu_order] = array('menu_id' => $menu_id, 'object_id' => $object_id ); } // order by the the custom menu as seen in appearance > menus ksort($menuids); // rebuid & the array $return = array(); foreach ($menuids as $order => $ids) { $return[$ids['menu_id']] = $ids['object_id']; } return $return; }
Comments