wp_set_post_categories()
wp_set_post_categories( int $post_ID, array|int $post_c…
wp_set_post_categories( int $post_ID, array|int $post_categories = array(), bool $append = false )
为文章设置类别。
Set categories for a post.
为文章设置类别。
Set categories for a post.
目录锚点:#说明#参数#源码#笔记
说明(Description)
如果未设置post categories参数,则使用默认类别。
参数(Parameters)
| 参数 | 类型 | 说明 |
|---|---|---|
| $post_ID | (int) | Post ID.不默认为全局$Post的ID。默认值为0。 |
| $post_categories | (array | int) | 类别ID的列表,或单个类别的ID。 |
| $append | (bool) | 如果为true,则不要删除现有类别,只需添加。如果为false,则将类别替换为新类别。 |
源码(Source)
/**
* Set categories for a post.
*
* If the post categories parameter is not set, then the default category is
* going used.
*
* @since 2.1.0
*
* @param int $post_ID Optional. The Post ID. Does not default to the ID
* of the global $post. Default 0.
* @param array|int $post_categories Optional. List of categories or ID of category.
* Default empty array.
* @param bool $append If true, don't delete existing categories, just add on.
* If false, replace the categories with the new categories.
* @return array|bool|WP_Error
*/
function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) {
$post_ID = (int) $post_ID;
$post_type = get_post_type( $post_ID );
$post_status = get_post_status( $post_ID );
// If $post_categories isn't already an array, make it one:
$post_categories = (array) $post_categories;
if ( empty( $post_categories ) ) {
if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
$post_categories = array( get_option('default_category') );
$append = false;
} else {
$post_categories = array();
}
} elseif ( 1 == count( $post_categories ) && '' == reset( $post_categories ) ) {
return true;
}
return wp_set_post_terms( $post_ID, $post_categories, 'category', $append );
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 2.1.0 | wp-includes/post.php | 19 | 8 |
笔记(Notes)
如果要以编程方式将文章从一个类别移动到另一个类别:
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!