函数文档

wp_create_category()

💡 云策文档标注

概述

wp_create_category() 函数用于在数据库中创建新分类(如果尚不存在)。它是 wp_insert_category() 的简化包装器,适用于基本分类创建场景。

关键要点

  • 参数:$cat_name(必需,分类名称,类型为 int 或 string),$category_parent(可选,父分类 ID,默认为 0)。
  • 返回值:成功时返回分类 ID(整数),失败时返回 0 或 WP_Error 对象。
  • 功能特性:如果分类已存在,不会重复创建,而是返回现有分类的 ID。
  • 限制:作为包装器,不适合处理复杂的自定义分类法元素。

代码示例

$category_name = 'New Category';
$parent_category_id = 3;
$category_id = wp_create_category( $category_name, $parent_category_id );
if ( ! is_wp_error( $category_id ) ) {
    echo 'Category created with ID: ' . $category_id;
} else {
    echo 'Error creating category: ' . $category_id->get_error_message();
}

注意事项

  • 使用前需确保分类不存在,否则会直接返回现有 ID。
  • 对于高级需求,建议直接使用 wp_insert_category() 函数。

📄 原文内容

Adds a new category to the database if it does not already exist.

Parameters

$cat_nameint|stringrequired
Category name.
$category_parentintoptional
ID of parent category.

Return

int|WP_Error

More Information

Parameters:

  • $cat_name: Name for the new category.
  • $parent: Category ID of the parent category.

Returns:

  • 0 on failure, category id on success.

wp_create_category() is a thin wrapper around wp_insert_category().

Because this is a wrapper, it is not suitable for entering a complex custom taxonomy element.

If the category already exists, it is not duplicated. The ID of the original existing category is returned without error.

Source

function wp_create_category( $cat_name, $category_parent = 0 ) {
	$id = category_exists( $cat_name, $category_parent );
	if ( $id ) {
		return $id;
	}

	return wp_insert_category(
		array(
			'cat_name'        => $cat_name,
			'category_parent' => $category_parent,
		)
	);
}

Changelog

Version Description
2.0.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    An example of how you can use the wp_create_category() function.

    $category_name = 'New Category'; // Name of the category you want to create
    $parent_category_id = 3; // ID of the parent category (change this to your desired parent category)
    
    // Attempt to create a new category
    $category_id = wp_create_category( $category_name, $parent_category_id );
    
    if ( ! is_wp_error( $category_id ) ) {
        echo 'Category created with ID: ' . $category_id;
    } else {
        echo 'Error creating category: ' . $category_id->get_error_message();
    }

    In this example:

    $category_name is set to “New Category”, which is the name of the category you want to create.
    $parent_category_id is set to 3, which is the ID of the parent category where you want to create the new category. You should replace this with the actual ID of the desired parent category.
    The wp_create_category() function is called with both the category name and the parent category ID as arguments. If the category is successfully created, it will return the category ID as an integer. If there’s an error during the creation process, it will return a WP_Error object.

    The code then checks if the result is a WP_Error object using is_wp_error(). If it’s not an error, it means the category was created successfully, and it will display a message with the category ID. If an error occurred, it will display an error message using the get_error_message() method of the WP_Error object.

  2. Skip to note 4 content

    Examples

    In order to create a simple category use:

     wp_create_category( 'My category name' );

    To create a category that is a child of Uncategorized (or whatever category has the ID 0), use:

    wp_create_category( 'Child of Uncategorized', 0 );

    To get id of category created and put value in variable:

    $id = wp_create_category( 'Child of Uncategorized', 0 );