将类别添加到上传的图像媒体-WordPress

我正在尝试使用Wordpress Rest API上传媒体(图像)。这是我的代码:

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('cover', 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');
}

并且我想向其添加海报类别一切正常, 并且图像已上传, 但不适用于该类别。对于图像的类别, 我正在使用此插件媒体库类别。


#1


wp_set_object_terms($attachment_id, 'poster', 'category', true);

就是答案。 https://developer.wordpress.org/reference/functions/wp_set_object_terms/

来源:

https://www.srcmini02.com/64680.html

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?