WordPress函数wp_get_nav_menu_items()

描述:

获取在后台自定义的导航菜单项

用法:

 <?php $items = wp_get_nav_menu_items( $menu, $args ); ?> 

参数:

$menu

(string) (必填) 菜单的 ‘id’,’name’ 或 ‘slug’

默认值: None

导航菜单位置不是有效参数。例如:如果使用函数register_nav_menus()注册了nav menu,并向函数传递一个类似array(’menu_1’=>u(’Menu 1’,’textdomain’)的参数,则location slug’menu_1’不是有效的参数。请检查下面的示例代码以了解其工作原理。

$args

(array) (可选) 可选参数

默认值: None

返回值:

扩展WP_Post对象的索引数组(如果菜单不包含项,则为空)或bool false。
下面是返回的对象的示例。这是一个子菜单项,类别链接,具有自定义导航标签、标题属性、CSS类、链接关系和说明:

  Object (
       ID = 2178
       post_author = "1"
       post_date = "2013-08-20 20:37:40"
       post_date_gmt = "2013-08-20 20:37:40"
       post_content = "This is the link description"
       post_title = "Example Category"
       post_excerpt = "My title attribute"
       post_status = "publish"
       comment_status = "open"
       ping_status = "open"
       post_password = ""
       post_name = "2178"
       to_ping = ""
       pinged = ""
       post_modified = "2013-08-20 20:13:08"
       post_modified_gmt = "2013-08-20 20:13:08"
       post_content_filtered = ""
       post_parent = 12
       guid = "https://example.com/?p=2178"
       menu_order = 2
       post_type = "nav_menu_item"
       post_mime_type = ""
       comment_count = "0"
       filter = "raw"
       format_content = NULL
       db_id = 2178
       menu_item_parent = "2177"
       object_id = "19"
       object = "category"
       type = "taxonomy"
       type_label = "Category"
       url = "https://example.com/category/example-category/"
       title = "Example Category"
       target = ""
       attr_title = "My title attribute"
       description = "This is the link description"
       classes => Array (
           ['0'] = "my-class"
       )
       xfn = "contact"
   )

示例:


    // 基于 $menu_name 获取导航菜单 (same as 'theme_location' or 'menu' arg to wp_nav_menu)
    // 此代码基于wp_nav_menu通过菜单别名获取菜单ID的代码
    $menu_name = 'custom_menu_slug';

    if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
	$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );

	$menu_items = wp_get_nav_menu_items($menu->term_id);

	$menu_list = '<ul id="menu-' . $menu_name . '">';

	foreach ( (array) $menu_items as $key => $menu_item ) {
	    $title = $menu_item->title;
	    $url = $menu_item->url;
	    $menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
	}
	$menu_list .= '</ul>';
    } else {
	$menu_list = '<ul><li>Menu "' . $menu_name . '" not defined.</li></ul>';
    }
    // $menu_list now ready to output

源文件:

function wp_get_nav_menu_items( $menu, $args = array() ) {
    $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 ( is_wp_error( $items ) ) {
        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,
    );
    $args            = wp_parse_args( $args, $defaults );
    $args['include'] = $items;
 
    if ( ! empty( $items ) ) {
        $items = get_posts( $args );
    } else {
        $items = array();
    }
 
    // 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(
                    array(
                        'taxonomy'     => $taxonomy,
                        'include'      => $terms[ $taxonomy ],
                        'hierarchical' => false,
                    )
                );
            }
        }
        unset( $terms );
    }
 
    $items = array_map( 'wp_setup_nav_menu_item', $items );
 
    if ( ! is_admin() ) { // Remove invalid items only in 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 );
}