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

时间:2018-10-10 22:16:14

标签: wordpress wordpress-theming custom-wordpress-pages wordpress-rest-api

我正在尝试使用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');
}

,我想向其中添加poster类别 一切正常,图像已上传,但不适用于该类别。  对于图像的类别,我正在使用此插件Media Library Categories

1 个答案:

答案 0 :(得分:0)

>>> min(L[::2])
143788.505

>>> min(L[1::2])
523760.528

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