返回介绍

wp_get_nav_menu_to_edit()

发布于 2017-09-11 12:06:49 字数 3933 浏览 1096 评论 0 收藏 0

wp_get_nav_menu_to_edit( int $menu_id )

Returns the menu formatted to edit.


description


参数

$menu_id

(int) (Optional) The ID of the menu to format. Default 0.


返回值

(string|WP_Error) $output The menu formatted to edit or error object on failure.


源代码

File: wp-admin/includes/nav-menu.php

function wp_get_nav_menu_to_edit( $menu_id = 0 ) {
	$menu = wp_get_nav_menu_object( $menu_id );

	// If the menu exists, get its items.
	if ( is_nav_menu( $menu ) ) {
		$menu_items = wp_get_nav_menu_items( $menu->term_id, array('post_status' => 'any') );
		$result = '<div id="menu-instructions" class="post-body-plain';
		$result .= ( ! empty($menu_items) ) ? ' menu-instructions-inactive">' : '">';
		$result .= '<p>' . __( 'Add menu items from the column on the left.' ) . '</p>';
		$result .= '</div>';

		if ( empty($menu_items) )
			return $result . ' <ul class="menu" id="menu-to-edit"> </ul>';

		/**
		 * Filters the Walker class used when adding nav menu items.
		 *
		 * @since 3.0.0
		 *
		 * @param string $class   The walker class to use. Default 'Walker_Nav_Menu_Edit'.
		 * @param int    $menu_id ID of the menu being rendered.
		 */
		$walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id );

		if ( class_exists( $walker_class_name ) ) {
			$walker = new $walker_class_name;
		} else {
			return new WP_Error( 'menu_walker_not_exist',
				/* translators: %s: walker class name */
				sprintf( __( 'The Walker class named %s does not exist.' ),
					'<strong>' . $walker_class_name . '</strong>'
				)
			);
		}

		$some_pending_menu_items = $some_invalid_menu_items = false;
		foreach ( (array) $menu_items as $menu_item ) {
			if ( isset( $menu_item->post_status ) && 'draft' == $menu_item->post_status )
				$some_pending_menu_items = true;
			if ( ! empty( $menu_item->_invalid ) )
				$some_invalid_menu_items = true;
		}

		if ( $some_pending_menu_items ) {
			$result .= '<div class="notice notice-info notice-alt inline"><p>' . __( 'Click Save Menu to make pending menu items public.' ) . '</p></div>';
		}

		if ( $some_invalid_menu_items ) {
			$result .= '<div class="notice notice-error notice-alt inline"><p>' . __( 'There are some invalid menu items. Please check or delete them.' ) . '</p></div>';
		}

		$result .= '<ul class="menu" id="menu-to-edit"> ';
		$result .= walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $menu_items), 0, (object) array('walker' => $walker ) );
		$result .= ' </ul> ';
		return $result;
	} elseif ( is_wp_error( $menu ) ) {
		return $menu;
	}

}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-admin/includes/nav-menu.php: wp_edit_nav_menu_walker
  • wp-includes/l10n.php: __()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/nav-menu-template.php: walk_nav_menu_tree()
  • wp-includes/nav-menu.php: wp_get_nav_menu_items()
  • wp-includes/nav-menu.php: wp_get_nav_menu_object()
  • wp-includes/nav-menu.php: is_nav_menu()
  • wp-includes/load.php: is_wp_error()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 4 more uses Hide more uses

User Contributed Notes

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

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

发布评论

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