返回介绍

wp_get_nav_menu_items()

发布于 2017-09-11 12:06:39 字数 13304 浏览 875 评论 0 收藏 0

wp_get_nav_menu_items( string $menu,  array $args = array() )

Return all menu items of a navigation menu.


description


参数

$menu

(string) (Required) Menu name, ID, or slug.

$args

(array) (Optional) Arguments to pass to get_posts().

Default value: array()


返回值

(false|array) $items Array of menu items, otherwise false.


源代码

File: wp-includes/nav-menu.php

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( $taxonomy, array(
					'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 );
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: wp_list_sort()
  • wp-includes/load.php: is_admin()
  • wp-includes/load.php: wp_using_ext_object_cache()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/taxonomy.php: get_terms()
  • wp-includes/taxonomy.php: get_objects_in_term()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post.php: get_posts()
  • wp-includes/post.php: get_post_meta()
  • wp-includes/nav-menu.php: wp_get_nav_menu_items
  • wp-includes/nav-menu.php: wp_get_nav_menu_object()
  • wp-includes/load.php: is_wp_error()
  • Show 7 more uses Hide more uses

Used By

  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::customize_register()
  • wp-admin/includes/nav-menu.php: wp_nav_menu_update_menu_items()
  • wp-admin/includes/nav-menu.php: wp_get_nav_menu_to_edit()
  • wp-includes/nav-menu-template.php: wp_nav_menu()
  • wp-includes/nav-menu.php: _wp_auto_add_pages_to_menu()
  • wp-includes/nav-menu.php: wp_update_nav_menu_item()
  • Show 1 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 4You must log in to vote on the helpfulness of this note Contributed by Stefano

    Building bootstrap 3 menu with submenu items without use WP_nav_walker (boostrap)! (Require bootstrap.css and bootstrap.js)

    
    <?php
    // Intented to use bootstrap 3.
    // Location is like a 'primary'
    // After, you print menu just add create_bootstrap_menu("primary") in your preferred position;
     
    #add this function in your theme functions.php
     
    function create_bootstrap_menu( $theme_location ) {
        if ( ($theme_location) && ($locations = get_nav_menu_locations()) && isset($locations[$theme_location]) ) {
    		
    		$menu_list  = '<nav class="navbar navbar-default">' ."\n";
            $menu_list .= '<div class="container-fluid">' ."\n";
            $menu_list .= '<!-- Brand and toggle get grouped for better mobile display -->' ."\n";
            $menu_list .= '<div class="navbar-header">' ."\n";
            $menu_list .= '<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">' ."\n";
            $menu_list .= '<span class="sr-only">Toggle navigation</span>' ."\n";
            $menu_list .= '<span class="icon-bar"></span>' ."\n";
            $menu_list .= '<span class="icon-bar"></span>' ."\n";
            $menu_list .= '<span class="icon-bar"></span>' ."\n";
            $menu_list .= '</button>' ."\n";
    		$menu_list .= '<a class="navbar-brand" href="' . home_url() . '">' . get_bloginfo( 'name' ) . '</a>';
            $menu_list .= '</div>' ."\n";
              
            $menu_list .= '<!-- Collect the nav links, forms, and other content for toggling -->';
    		
    		
            $menu = get_term( $locations[$theme_location], 'nav_menu' );
            $menu_items = wp_get_nav_menu_items($menu->term_id);
    
            $menu_list .= '<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">' ."\n";
            $menu_list .= '<ul class="nav navbar-nav">' ."\n";
             
            foreach( $menu_items as $menu_item ) {
    			if( $menu_item->menu_item_parent == 0 ) {
    				
    				$parent = $menu_item->ID;
    				
    				$menu_array = array();
    				foreach( $menu_items as $submenu ) {
    					if( $submenu->menu_item_parent == $parent ) {
    $bool = true;
    $menu_array[] = '<li><a href="' . $submenu->url . '">' . $submenu->title . '</a></li>' ."\n";
    					}
    				}
    				if( $bool == true && count( $menu_array ) > 0 ) {
    					
    					$menu_list .= '<li class="dropdown">' ."\n";
    					$menu_list .= '<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $menu_item->title . ' <span class="caret"></span></a>' ."\n";
    					
    					$menu_list .= '<ul class="dropdown-menu">' ."\n";
    					$menu_list .= implode( "\n", $menu_array );
    					$menu_list .= '</ul>' ."\n";
    					
    				} else {
    					
    					$menu_list .= '<li>' ."\n";
    					$menu_list .= '<a href="' . $menu_item->url . '">' . $menu_item->title . '</a>' ."\n";
    				}
    				
    			}
    			
    			// end <li>
    			$menu_list .= '</li>' ."\n";
            }
             
            $menu_list .= '</ul>' ."\n";
            $menu_list .= '</div>' ."\n";
    		$menu_list .= '</div><!-- /.container-fluid -->' ."\n";
            $menu_list .= '</nav>' ."\n";
     
        } else {
            $menu_list = '<!-- no menu defined in location "'.$theme_location.'" -->';
        }
    	
    	echo $menu_list;
    }
    ?>
    
  2. Building menu list with children (submenus) and selecting the menu by it’s location:

    
    // Intented to use with locations, like 'primary'
    // clean_custom_menu("primary");
    
    #add in your theme functions.php file
    
    function clean_custom_menu( $theme_location ) {
    	if ( ($theme_location) && ($locations = get_nav_menu_locations()) && isset($locations[$theme_location]) ) {
    		$menu = get_term( $locations[$theme_location], 'nav_menu' );
    		$menu_items = wp_get_nav_menu_items($menu->term_id);
    
    		$menu_list  = '<nav>' ."\n";
    		$menu_list .= '<ul class="main-nav">' ."\n";
    
    		$count = 0;
    		$submenu = false;
    		
    		foreach( $menu_items as $menu_item ) {
    			
    			$link = $menu_item->url;
    			$title = $menu_item->title;
    			
    			if ( !$menu_item->menu_item_parent ) {
    				$parent_id = $menu_item->ID;
    				
    				$menu_list .= '<li class="item">' ."\n";
    				$menu_list .= '<a href="'.$link.'" class="title">'.$title.'</a>' ."\n";
    			}
    
    			if ( $parent_id == $menu_item->menu_item_parent ) {
    
    				if ( !$submenu ) {
    					$submenu = true;
    					$menu_list .= '<ul class="sub-menu">' ."\n";
    				}
    
    				$menu_list .= '<li class="item">' ."\n";
    				$menu_list .= '<a href="'.$link.'" class="title">'.$title.'</a>' ."\n";
    				$menu_list .= '</li>' ."\n";
    					
    
    				if ( $menu_items[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ){
    					$menu_list .= '</ul>' ."\n";
    					$submenu = false;
    				}
    
    			}
    
    			if ( $menu_items[ $count + 1 ]->menu_item_parent != $parent_id ) { 
    				$menu_list .= '</li>' ."\n";      
    				$submenu = false;
    			}
    
    			$count++;
    		}
    		
    		$menu_list .= '</ul>' ."\n";
    		$menu_list .= '</nav>' ."\n";
    
    	} else {
    		$menu_list = '<!-- no menu defined in location "'.$theme_location.'" -->';
    	}
    	echo $menu_list;
    }
    

    Building simple menu list

    
    // Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu)
    // This code based on wp_nav_menu's code to get Menu ID from menu slug
    
    $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
    
    

    Get simple array of menu.

    
    function wp_get_menu_array($current_menu) {
    
        $array_menu = wp_get_nav_menu_items($current_menu);
    	$menu = array();
    	foreach ($array_menu as $m) {
    		if (empty($m->menu_item_parent)) {
    			$menu[$m->ID] = array();
    			$menu[$m->ID]['ID'] 		= 	$m->ID;
    			$menu[$m->ID]['title'] 		= 	$m->title;
    			$menu[$m->ID]['url'] 		= 	$m->url;
    			$menu[$m->ID]['children']	= 	array();
    		}
    	}
    	$submenu = array();
    	foreach ($array_menu as $m) {
    		if ($m->menu_item_parent) {
    			$submenu[$m->ID] = array();
    			$submenu[$m->ID]['ID'] 		= 	$m->ID;
    			$submenu[$m->ID]['title']	= 	$m->title;
    			$submenu[$m->ID]['url'] 	= 	$m->url;
    			$menu[$m->menu_item_parent]['children'][$m->ID] = $submenu[$m->ID];
    		}
    	}
        return $menu;
        
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文