为了在外部上传图片,将这些代码粘贴到您的functions.php中
function uploadImage($new_post){
$inc = 1;
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$new_post);
// $newupload returns the attachment id of the file that
// was just uploaded. Do whatever you want with that now.
$inc++;
}
}
}
function insert_attachment($file_handler,$post_id) {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) return false;
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
}
并使用您的新帖子 ID 在您的模板中调用 uploadimage 函数
uploadImage($the_post_id);
只需输入五个上传输入表单,它会自动被函数读取