1. 首页
  2. WordPress 函数手册

wp_insert_category()

wp_insert_category( array $catarr, bool $wp_error = false )

更新现有类别或创建新类别。
Updates an existing Category or creates a new Category.

目录锚点:#源码#笔记


源码(Source)

/**
 * Updates an existing Category or creates a new Category.
 *
 * @since 2.0.0
 * @since 2.5.0 $wp_error parameter was added.
 * @since 3.0.0 The 'taxonomy' argument was added.
 *
 * @param array $catarr {
 *     Array of arguments for inserting a new category.
 *
 *     @type int        $cat_ID               Categoriy ID. A non-zero value updates an existing category.
 *                                            Default 0.
 *     @type string     $taxonomy             Taxonomy slug. Defualt 'category'.
 *     @type string     $cat_name             Category name. Default empty.
 *     @type string     $category_description Category description. Default empty.
 *     @type string     $category_nicename    Category nice (display) name. Default empty.
 *     @type int|string $category_parent      Category parent ID. Default empty.
 * }
 * @param bool  $wp_error Optional. Default false.
 * @return int|object The ID number of the new or updated Category on success. Zero or a WP_Error on failure,
 *                    depending on param $wp_error.
 */
function wp_insert_category( $catarr, $wp_error = false ) {
	$cat_defaults = array( 'cat_ID' => 0, 'taxonomy' => 'category', 'cat_name' => '', 'category_description' => '', 'category_nicename' => '', 'category_parent' => '' );
	$catarr = wp_parse_args( $catarr, $cat_defaults );

	if ( trim( $catarr['cat_name'] ) == '' ) {
		if ( ! $wp_error ) {
			return 0;
		} else {
			return new WP_Error( 'cat_name', __( 'You did not enter a category name.' ) );
		}
	}

	$catarr['cat_ID'] = (int) $catarr['cat_ID'];

	// Are we updating or creating?
	$update = ! empty ( $catarr['cat_ID'] );

	$name = $catarr['cat_name'];
	$description = $catarr['category_description'];
	$slug = $catarr['category_nicename'];
	$parent = (int) $catarr['category_parent'];
	if ( $parent < 0="" )="" {="" $parent="0;" }="" if="" (="" empty(="" $parent="" )="" ||="" !="" term_exists(="" $parent,="" $catarr&#91;'taxonomy'&#93;="" )="" ||="" (="" $catarr&#91;'cat_id'&#93;="" &&="" term_is_ancestor_of(="" $catarr&#91;'cat_id'&#93;,="" $parent,="" $catarr&#91;'taxonomy'&#93;="" )="" )="" )="" {="" $parent="0;" }="" $args="compact('name'," 'slug',="" 'parent',="" 'description');="" if="" (="" $update="" )="" {="" $catarr&#91;'cat_id'&#93;="wp_update_term(" $catarr&#91;'cat_id'&#93;,="" $catarr&#91;'taxonomy'&#93;,="" $args="" );="" }="" else="" {="" $catarr&#91;'cat_id'&#93;="wp_insert_term(" $catarr&#91;'cat_name'&#93;,="" $catarr&#91;'taxonomy'&#93;,="" $args="" );="" }="" if="" (="" is_wp_error(="" $catarr&#91;'cat_id'&#93;="" )="" )="" {="" if="" (="" $wp_error="" )="" {="" return="" $catarr&#91;'cat_id'&#93;;="" }="" else="" {="" return="" 0;="" }="" }="" return="" $catarr&#91;'cat_id'&#93;&#91;'term_id'&#93;;="" }="">
更新版本 源码位置 使用 被使用
3.0.0 wp-admin/includes/taxonomy.php 1 16

笔记(Notes)

插入新类别:

wp_insert_category() 为WP2原创文章,链接:https://www.wp2.cn/functions/wp_insert_category/