返回介绍

wp_get_nav_menu_object()

发布于 2017-09-11 12:06:45 字数 3657 浏览 965 评论 0 收藏 0

wp_get_nav_menu_object( int|string|WP_Term $menu )

Returns a navigation menu object.


description


参数

$menu

(int|string|WP_Term) (Required) Menu ID, slug, or name - or the menu object.


返回值

(WP_Term|false) False if $menu param isn't supplied or term does not exist, menu object if successful.


源代码

File: wp-includes/nav-menu.php

function wp_get_nav_menu_object( $menu ) {
	$menu_obj = false;

	if ( is_object( $menu ) ) {
		$menu_obj = $menu;
	}

	if ( $menu && ! $menu_obj ) {
		$menu_obj = get_term( $menu, 'nav_menu' );

		if ( ! $menu_obj ) {
			$menu_obj = get_term_by( 'slug', $menu, 'nav_menu' );
		}

		if ( ! $menu_obj ) {
			$menu_obj = get_term_by( 'name', $menu, 'nav_menu' );
		}
	}

	if ( ! $menu_obj || is_wp_error( $menu_obj ) ) {
		$menu_obj = false;
	}

	/**
	 * Filters the nav_menu term retrieved for wp_get_nav_menu_object().
	 *
	 * @since 4.3.0
	 *
	 * @param object|false $menu_obj Term from nav_menu taxonomy, or false if nothing had been found.
	 * @param string       $menu     The menu ID, slug, or name passed to wp_get_nav_menu_object().
	 */
	return apply_filters( 'wp_get_nav_menu_object', $menu_obj, $menu );
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/nav-menu.php: wp_get_nav_menu_object
  • wp-includes/taxonomy.php: get_term_by()
  • wp-includes/taxonomy.php: get_term()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/load.php: is_wp_error()

Used By

  • wp-includes/customize/class-wp-customize-nav-menu-setting.php: WP_Customize_Nav_Menu_Setting::value()
  • wp-admin/includes/nav-menu.php: wp_get_nav_menu_to_edit()
  • wp-includes/widgets/class-wp-nav-menu-widget.php: WP_Nav_Menu_Widget::widget()
  • wp-includes/nav-menu-template.php: wp_nav_menu()
  • wp-includes/nav-menu.php: wp_delete_nav_menu()
  • wp-includes/nav-menu.php: wp_update_nav_menu_object()
  • wp-includes/nav-menu.php: wp_update_nav_menu_item()
  • wp-includes/nav-menu.php: wp_get_nav_menu_items()
  • wp-includes/nav-menu.php: is_nav_menu()
  • Show 4 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: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Examples

    
    // By name.
    $menu = wp_get_nav_menu_object( 'wpdocs mainmenu' );
    
    // By slug.
    $menu = wp_get_nav_menu_object( 'wpdocs-mainmenu' );
    
    // By ID.
    $menu_name = 'wpdocs mainmenu';
    $menu_obj  = get_term_by( 'name', $menu_name, 'nav_menu' );
    $menu_id   = $menu_obj->term_id;
    $menu = wp_get_nav_menu_object( $menu_id );
    
    
    // By location.
    $menu_name = 'primary';
    $locations = get_nav_menu_locations();
    $menu_id   = $locations[ $menu_name ] ;
    wp_get_nav_menu_object( $menu_id );
    

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

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

发布评论

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