wp_insert_category()

wp_insert_category( array $catarr, bool $wp_error = fal…

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['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'];="" }="">
更新版本 源码位置 使用 被使用
3.0.0 wp-admin/includes/taxonomy.php 1 16

笔记(Notes)

插入新类别:

类别:WordPress 函数手册

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT

登录 账号发表你的看法,还没有账号?立即免费 注册