wp_get_nav_menu_items

函数
wp_get_nav_menu_items ( $menu, $args = array() )
参数
  • (int|string|WP_Term) $menu Menu ID, slug, name, or object.
    Required:
  • (array) $args { Optional. Arguments to pass to get_posts(). @type string $order How to order nav menu items as queried with get_posts(). Will be ignored if 'output' is ARRAY_A. Default 'ASC'. @type string $orderby Field to order menu items by as retrieved from get_posts(). Supply an orderby field via 'output_key' to affect the output order of nav menu items. Default 'menu_order'. @type string $post_type Menu items post type. Default 'nav_menu_item'. @type string $post_status Menu items post status. Default 'publish'. @type string $output How to order outputted menu items. Default ARRAY_A. @type string $output_key Key to use for ordering the actual menu items that get returned. Note that that is not a get_posts() argument and will only affect output of menu items processed in this function. Default 'menu_order'. @type bool $nopaging Whether to retrieve all menu items (true) or paginate (false). Default true. }
    Required:
    Default: array()
返回值
  • (array|false) Array of menu items, otherwise false.
定义位置
相关方法
wp_setup_nav_menu_itemwp_save_nav_menu_itemswp_get_nav_menus_sort_nav_menu_itemswp_get_nav_menu_name
引入
3.0.0
弃用
-

wp_get_nav_menu_items: 这个函数为一个特定的菜单检索菜单项。它需要一个参数,就是菜单的ID或名称,并返回一个菜单项对象的数组。

检索一个导航菜单的所有菜单项。

注意:传递给`$args`参数的大多数参数–除了’output_key’–是专门用于从get_posts()检索nav_menu_item文章的,可能只是间接地影响从这个函数返回的导航菜单项的最终排序和内容。

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

	if ( ! $menu ) {
		return false;
	}

	if ( ! taxonomy_exists( 'nav_menu' ) ) {
		return false;
	}

	$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_menu_item_cache' => true,
		'tax_query'              => array(
			array(
				'taxonomy' => 'nav_menu',
				'field'    => 'term_taxonomy_id',
				'terms'    => $menu->term_taxonomy_id,
			),
		),
	);
	$args     = wp_parse_args( $args, $defaults );
	if ( $menu->count > 0 ) {
		$items = get_posts( $args );
	} else {
		$items = array();
	}

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

	if ( ! is_admin() ) { // Remove invalid items only on front end.
		$items = array_filter( $items, '_is_valid_nav_menu_item' );
	}

	if ( ARRAY_A === $args['output'] ) {
		$items = wp_list_sort(
			$items,
			array(
				$args['output_key'] => 'ASC',
			)
		);

		$i = 1;

		foreach ( $items as $k => $item ) {
			$items[ $k ]->{$args['output_key']} = $i++;
		}
	}

	/**
	 * Filters the navigation menu items being returned.
	 *
	 * @since 3.0.0
	 *
	 * @param array  $items An array of menu item post objects.
	 * @param object $menu  The menu object.
	 * @param array  $args  An array of arguments used to retrieve menu item objects.
	 */
	return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
}

常见问题

FAQs
查看更多 >