wp_create_categories() WP 2.0.0
Create categories for the given post.
Хуков нет.
Возвращает
Число[]. Array of IDs of categories assigned to the given post.
Использование
wp_create_categories( $categories, $post_id );
- $categories(строка[]) (обязательный)
- Array of category names to create.
- $post_id(число)
- The post ID.
По умолчанию: ''
Список изменений
С версии 2.0.0 | Введена. |
Код wp_create_categories() wp create categories WP 5.6.2
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;
}