Advertisements

wp_get_nav_menu_items

Definition:
function wp_get_nav_menu_items( $menu, $args = array() {}

Returns all menu items of a navigation menu.

Parameters

  • string $menu: menu name, id, or slug
  • string $args

Return values

returns:array of menu items, else false.

Defined filters

  • wp_get_nav_menu_items
    apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args )

Source code

function wp_get_nav_menu_items( $menu, $args = array() ) {

	global $_wp_using_ext_object_cache;



	$menu = wp_get_nav_menu_object( $menu );



	if ( ! $menu )

		return false;



	static $fetched = array();



	$items = get_objects_in_term( $menu->term_id, 'nav_menu' );



	if ( empty( $items ) )

		return $items;



	$defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',

		'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true,

		'update_post_term_cache' => false );

	$args = wp_parse_args( $args, $defaults );

	if ( count( $items ) > 1 )

		$args['include'] = implode( ',', $items );

	else

		$args['include'] = $items[0];



	$items = get_posts( $args );



	if ( is_wp_error( $items ) || ! is_array( $items ) )

		return false;



	// Get all posts and terms at once to prime the caches

	if ( empty( $fetched[$menu->term_id] ) || $_wp_using_ext_object_cache ) {

		$fetched[$menu->term_id] = true;

		$posts = array();

		$terms = array();

		foreach ( $items as $item ) {

			$object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );

			$object    = get_post_meta( $item->ID, '_menu_item_object',    true );

			$type      = get_post_meta( $item->ID, '_menu_item_type',      true );



			if ( 'post_type' == $type )

				$posts[$object][] = $object_id;

			elseif ( 'taxonomy' == $type)

				$terms[$object][] = $object_id;

		}



		if ( ! empty( $posts ) ) {

			foreach ( array_keys($posts) as $post_type ) {

				get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) );

			}

		}

		unset($posts);



		if ( ! empty( $terms ) ) {

			foreach ( array_keys($terms) as $taxonomy ) {

				get_terms($taxonomy, array('include' => $terms[$taxonomy]) );

			}

		}

		unset($terms);

	}



	$items = array_map( 'wp_setup_nav_menu_item', $items );



	if ( ! is_admin() ) // Remove invalid items only in frontend

		$items = array_filter( $items, '_is_valid_nav_menu_item' );



	if ( ARRAY_A == $args['output'] ) {

		$GLOBALS['_menu_item_sort_prop'] = $args['output_key'];

		usort($items, '_sort_nav_menu_items');

		$i = 1;

		foreach( $items as $k => $item ) {

			$items[$k]->$args['output_key'] = $i++;

		}

	}



	return apply_filters( 'wp_get_nav_menu_items',  $items, $menu, $args );

}

3715

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: