• Portfolio
    • Posters & Logos
    • Websites
    • Galleries
    • Blog
    • About
  •  
  • View All
  • |
  • Design
  • Featured
  • HTML & CSS
  • Misc
  • PHP
  • Uncategorized
  • Wordpress
  •  Saturday August 27th, 2011
    Posted in: PHP & Wordpress
    Share: Twitter ∞ Facebook
    No comments ∞
  • 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;
    }


    ← WordPress WPIndexatic Theme
    Making of: Bass on cloud nine →


    Comments

    Comments closed.

 
  • David Sword
    David Sword
    Digital Artist & Developer
    davida @daavid_ sword.cacom
    Always having the same reoccurring thought at gym... "damnit, forgot to wear deodorant"
    3 hours ago.

    Follow @david_sword

  • Contact Me


  • Online Print Store
    • limited edition
    • hand signed
    • seq. numbered
    • qlty paper+ink
    • never reprints
    • Paypal/Interac
    Visit Store! →

    Coming soon.

  • Instagram
    • Good eatin

© Copyright 2006 - 2012 David Sword. All Rights Reserved   ∞   Credit & Resources   ∞   Links   ∞   Proudly powered by Wordpress & heyyou   ∞   Mobile Site