下面的代码上载了一个我想与自定义帖子类型关联的图像。
$upload_overrides = array( \'test_form\' => FALSE );
if($file_errors[\'error\'] == 0) {
if($new_reseller_id = wp_insert_post( $new_reseller_profile_data )){
//$this->wps_process_image($file, $new_reseller_id);exit;
$uploaded_file = wp_handle_upload( $file, $upload_overrides );
$wp_filetype = wp_check_filetype(basename($uploaded_file[\'file\']), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
\'guid\' => $wp_upload_dir[\'url\'] . \'/\' . basename( $uploaded_file[\'file\'] ),
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($file[\'name\'])),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
$attach_id = wp_insert_attachment( $attachment, $uploaded_file[\'file\'], $new_reseller_id );
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_file[\'file\'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
}
图像已上载并可在图像库中查看,但不会在自定义帖子类型编辑器中显示为特征图像。为什么会这样?