将类别添加到上传的图像媒体-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

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_41273.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?