wp_create_categories

函式
wp_create_categories ( $categories, $post_id = '' )
引數
  • (string[]) $categories Array of category names to create.
    Required:
  • (int) $post_id Optional. The post ID. Default empty.
    Required:
    Default: (empty)
返回值
  • (int[]) Array of IDs of categories assigned to the given post.
定義位置
相關方法
wp_create_categorywp_list_categorieswp_create_termwp_get_post_categoriesget_categories
引入
2.0.0
棄用
-

wp_create_categories: 這是一個可以一次性建立多個分類的函式。它可以用來快速和容易地建立大量的類別。

為給定的文章建立分類。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_create_categories( $categories, $post_id = '' ) {
$cat_ids = array();
foreach ( $categories as $category ) {
$id = category_exists( $category );
if ( $id ) {
$cat_ids[] = $id;
} else {
$id = wp_create_category( $category );
if ( $id ) {
$cat_ids[] = $id;
}
}
}
if ( $post_id ) {
wp_set_post_categories( $post_id, $cat_ids );
}
return $cat_ids;
}
function wp_create_categories( $categories, $post_id = '' ) { $cat_ids = array(); foreach ( $categories as $category ) { $id = category_exists( $category ); if ( $id ) { $cat_ids[] = $id; } else { $id = wp_create_category( $category ); if ( $id ) { $cat_ids[] = $id; } } } if ( $post_id ) { wp_set_post_categories( $post_id, $cat_ids ); } return $cat_ids; }
function wp_create_categories( $categories, $post_id = '' ) {
	$cat_ids = array();
	foreach ( $categories as $category ) {
		$id = category_exists( $category );
		if ( $id ) {
			$cat_ids[] = $id;
		} else {
			$id = wp_create_category( $category );
			if ( $id ) {
				$cat_ids[] = $id;
			}
		}
	}

	if ( $post_id ) {
		wp_set_post_categories( $post_id, $cat_ids );
	}

	return $cat_ids;
}

常見問題

FAQs
檢視更多 >