您需要注册支持的自定义帖子类型category
分类法:
add_action(\'init\', \'cyb_register_post_type\');
function cyb_register_post_type() {
$args = array(
// All the other args
\'taxonomies\' => array( \'category\' ),
);
register_post_type( \'my_post_type\', $args );
}
然后可以设置自定义帖子类型和
categoy
分类法,但必须更正代码。
由此:
wp_set_post_categories( $post_id, $category_ids, \'category\');
为此(删除以前的类别并替换为新类别):
wp_set_post_categories( $post_id, $category_ids );
// The above line is equivalent to
// wp_set_post_categories( $post_id, $category_ids, false );
// or
// wp_set_post_terms( $post_id, $category_ids, \'category\', false );
或至(不删除以前的类别,添加新类别):
wp_set_post_categories( $post_id, $category_ids, true );
您还可以注册自定义分类法并将其用于自定义帖子类型:
add_action(\'init\', \'cyb_register_post_type_and_taxonomy\');
function cyb_register_post_type_and_taxonomy() {
$post_type_args = array(
// All the other args
\'taxonomies\' => array( \'my_custom_taxonomy\' ),
);
register_post_type( \'my_post_type\', $post_type_args );
$taxonomy_args = array(
// Arguments for the custom taxonomy
// See https://developer.wordpress.org/reference/functions/register_taxonomy/
);
register_taxonomy( \'my_custom_taxonomy\', \'my_post_type\', $args );
}
然后使用
wp_set_post_terms()
, 不
wp_set_post_categories()
:
wp_set_post_terms( $post_id, $category_ids, \'my_custom_taxonomy\');