wp_create_category()通过代码创建一个分类

目录

描述

This function adds a "simple" category by specifying a category name and (optionally) a category parent.

wp_create_category() 描述

用法

<?php wp_create_category$cat_name$parent ); ?>

wp_create_category() 用法

参数

$cat_name

(string) (必填) Name for the new category.

默认值: None

$parent

(integer) (可选) Category ID of the parent category.

默认值: None

wp_create_category() 参数

返回值

(integer) 

0 on failure, category id on success.

wp_create_category() 返回值

示例

In order to create a simple category use:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

 wp_create_category('My category name');

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

wp_create_category('Child of Uncategorized', 0);

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

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

wp_create_category() 示例

注意

  • 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.

wp_create_category() 注意

源文件

wp_create_category() 函数的代码位于 wp-admin/includes/taxonomy.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Add a new category to the database if it does not already exist.
 *
 * @since 2.0.0
 *
 * @param int|string $cat_name
 * @param int        $parent
 * @return int|WP_Error
 */
function wp_create_category( $cat_name, $parent = 0 ) {
	if ( $id = category_exists($cat_name, $parent) )
		return $id;

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

wp_create_category() 源文件