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

目录

描述

Inserts a new category into the taxonomy system.

wp_insert_category() 描述

用法

<?php wp_insert_category$catarr$wp_error ); ?>

wp_insert_category() 用法

参数

$catarr

(array) (必填) Category information.

默认值: None

$catarr is checked against an array with the following default values:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
$cat_defaults = array(
  'cat_ID' => 0,
  'cat_name' => ,
  'category_description' => ,
  'category_nicename' => ,
  'category_parent' => ,
  'taxonomy' => 'category' );

$catarr may contain additional values, but it is recommended that you provide at least the minimum of those defined in $cat_defaults. Note that category_parent is a category ID.

$wp_error

(boolean) (可选) Whether to return a WP_Error or a boolean. Boolean by default.

默认值: false

wp_insert_category() 参数

返回值

(boolean | WP_Error | int) 

True on success and false on failure OR a WP_Error on failure and the Category ID of the new category on true.

wp_insert_category() 返回值

示例

To insert a new category use:

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

%%%//Define the category
$my_cat = array('cat_name' => 'My Category', 'category_description' => 'A Cool Category', 'category_nicename' => 'category-slug', 'category_parent' => '');

// Create the category
$my_cat_id = wp_insert_category($my_cat);

wp_insert_category() 示例

注意

  • Not all possible members of the $catarr array are listed here.
  • See wp_create_category() for a simpler version which takes just a string instead of an array.

wp_insert_category() 注意

源文件

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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['taxonomy']="" )="" ||="" (="" $catarr['cat_id']="" &&="" term_is_ancestor_of(="" $catarr['cat_id'],="" $parent,="" $catarr['taxonomy']="" )="" )="" )="" {="" $parent="0;" }="" $args="compact('name'," 'slug',="" 'parent',="" 'description');="" if="" (="" $update="" )="" {="" $catarr['cat_id']="wp_update_term(" $catarr['cat_id'],="" $catarr['taxonomy'],="" $args="" );="" }="" else="" {="" $catarr['cat_id']="wp_insert_term(" $catarr['cat_name'],="" $catarr['taxonomy'],="" $args="" );="" }="" if="" (="" is_wp_error(="" $catarr['cat_id']="" )="" )="" {="" if="" (="" $wp_error="" )="" {="" return="" $catarr['cat_id'];="" }="" else="" {="" return="" 0;="" }="" }="" return="" $catarr['cat_id']['term_id'];="" }="">

wp_insert_category() 源文件