Skip to content

Instantly share code, notes, and snippets.

@gyrus
Created July 21, 2012 20:32
Show Gist options
  • Select an option

  • Save gyrus/3157067 to your computer and use it in GitHub Desktop.

Select an option

Save gyrus/3157067 to your computer and use it in GitHub Desktop.
Get menu items of WordPress nav menu attached to specified menu location
<?php
/**
* Get menu items of nav menu attached to specified menu location
*
* @uses get_nav_menu_locations()
*/
function pilau_get_menu_location_items( $location_slug, $max = 0 ) {
static $menu_locations = null;
if ( $menu_locations === null )
$menu_locations = get_nav_menu_locations();
$menu = $menu_locations[ $location_slug ];
$menu = wp_get_nav_menu_items( $menu );
if ( $max && count( $menu ) > $max )
$menu = array_slice( $menu, 0, $max );
$items = array();
foreach ( $menu as $menu_item )
$items[] = get_post( $menu_item->object_id );
return $items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment