wp_delete_nav_menu

函数
wp_delete_nav_menu ( $menu )
参数
  • (int|string|WP_Term) $menu Menu ID, slug, name, or object.
    Required:
返回值
  • (bool|WP_Error) True on success, false or WP_Error object on failure.
定义位置
相关方法
wp_create_nav_menuwp_get_nav_menuswp_nav_menu_wp_delete_tax_menu_itemwp_delete_attachment
引入
3.0.0
弃用
-

wp_delete_nav_menu:此函数从WordPress数据库中删除导航菜单。它将菜单ID作为参数,并从数据库中删除菜单的元数据和内容。

删除一个导航菜单。

function wp_delete_nav_menu( $menu ) {
	$menu = wp_get_nav_menu_object( $menu );
	if ( ! $menu ) {
		return false;
	}

	$menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
	if ( ! empty( $menu_objects ) ) {
		foreach ( $menu_objects as $item ) {
			wp_delete_post( $item );
		}
	}

	$result = wp_delete_term( $menu->term_id, 'nav_menu' );

	// Remove this menu from any locations.
	$locations = get_nav_menu_locations();
	foreach ( $locations as $location => $menu_id ) {
		if ( $menu_id == $menu->term_id ) {
			$locations[ $location ] = 0;
		}
	}
	set_theme_mod( 'nav_menu_locations', $locations );

	if ( $result && ! is_wp_error( $result ) ) {

		/**
		 * Fires after a navigation menu has been successfully deleted.
		 *
		 * @since 3.0.0
		 *
		 * @param int $term_id ID of the deleted menu.
		 */
		do_action( 'wp_delete_nav_menu', $menu->term_id );
	}

	return $result;
}

常见问题

FAQs
查看更多 >