编辑:在朋友的帮助下,我想出了一个解决方案。对于所有感兴趣的人:使用自定义帖子类型,在我的例子中是comment\\u post。然后上传如下图像:
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $comment,
\'post_status\' => \'pending\',// Choose: publish, preview, future, draft, etc.
\'post_type\' => \'comments_post\' // Use a custom post type
);
//save the new post and return its ID
$pid = wp_insert_post($new_post);
//Upload the file(s)
require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
if ($_FILES) {
foreach ($_FILES as $file => $array) {
//Check if the $_FILES is set and if the size is > 0 (if =0 it\'s empty)
if(isset($_FILES[$file]) && $_FILES[$file][\'size\']>0){
if ($_FILES[$file][\'error\'] !== UPLOAD_ERR_OK) {
echo \'<div class="allert alert-error"><p>Upload error : \' . $_FILES[$file][\'error\'] . \'</p></div>\';
$upload = false;
}else{
$upload = true;
}
if($upload == true){
$attach_id = media_handle_upload( $file, $pid );
}
}
}
}//End if \'$_FILES\'
}//End if errornumbers