我想上传一张图片并附加一个类别。这是我的代码:
function upload_cover(WP_REST_Request $request) {
require_once( ABSPATH . \'wp-admin/includes/image.php\' );
require_once( ABSPATH . \'wp-admin/includes/file.php\' );
require_once( ABSPATH . \'wp-admin/includes/media.php\' );
$attachment_id = media_handle_upload(\'poster\', 0);
$event = array(
\'post_status\' => \'publish\',
\'post_type\' => \'poster\',
\'meta_input\' => array(),
\'post_category\' => array(\'poster\')
);
$post_id = wp_insert_post( $event );
wp_set_post_terms($post_id, \'poster\', \'category\');
}
上传器工作正常,但图像没有附加任何类别。我尝试了以下方法:
$cat_id = get_cat_ID(\'cover\');
add_term_meta( $cat_id, \'poster\', $post_id, true );
wp_set_post_categories($post_id, array(\'poster\'), true);
wp_set_post_terms($post_id, array(\'poster\'), \'category\');
对于图像的类别,我使用这个插件
Media Library Categories
.