另一种方法是将上传表单<input type="file">
在前端过帐表单中;在函数的其余部分中/之后处理它。
以下是文件输入:
<input type="file" id="profile-picture" name="profile-picture[]" size="40" multiple />
只需相应地更改name属性。这个
[]
关于属性值(&A);这个
multiple
用于多文件上载。
附加功能:
function attach_uploads($uploads,$post_id = 0){
$files = rearrange($uploads);
if($files[0][\'name\']==\'\'){
return false;
}
foreach($files as $file){
$upload_file = wp_handle_upload( $file, array(\'test_form\' => false) );
$attachment = array(
\'post_mime_type\' => $upload_file[\'type\'],
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($upload_file[\'file\'])),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
$attach_id = wp_insert_attachment( $attachment, $upload_file[\'file\'], $post_id );
$attach_array[] = $attach_id;
require_once(ABSPATH . \'wp-admin/includes/image.php\');
$attach_data = wp_generate_attachment_metadata( $attach_id, $upload_file[\'file\'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
return $attach_array;
}
您可以找到有关重排功能的更多信息
here.
以及附加功能,以附加为post\\u缩略图
// put this along the other $_POST
$files = $_FILES[\'profile-picture\'];
// insert attachment, after you have created the new post id → $post_id
$attached_files = attach_uploads($files,$post_id);
// set the first file as post thumbnail
if($attached_files){
set_post_thumbnail( $post_id, $attached_files[0] );
}
希望这有帮助