谢谢@ifdion。。这就是我的最终功能。PHP代码
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) && $_POST[\'action\'] == "new_post") {
// Do some minor form validation to make sure there is content
$title = $_POST[\'title\'];
$ordertype = $_POST[\'ordertype\'];
$price = $_POST[\'price\'];
$ordertime = $_POST[\'ordertime\'];
$files = $_FILES[\'profile-picture\'];
// Add the content of the form to $post as an array
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'post_category\' => array($_POST[\'category\']), // Usable for custom taxonomies too
\'post_status\' => \'publish\', // Choose: publish, preview, future, draft, etc.
\'post_type\' => \'post\' //\'post\',page\' or use a custom post type if you want to
);
//save the new post
$pid = wp_insert_post($new_post);
add_post_meta($pid, \'ordertype\', $ordertype, true);
add_post_meta($pid, \'price\', $price, true);
add_post_meta($pid, \'ordertime\', $ordertime, true);
$attached_files = attach_uploads($files,$pid);
if($attached_files){
set_post_thumbnail( $pid, $attached_files[0] );
}
wp_redirect(get_permalink($pid)); exit;
//insert taxonomies
}
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;
}
并在主题中上载[输入]。。
<input type="file" id="profile-picture" name="profile-picture[]" size="40" multiple />
但如果我提交表单,并用此错误进行响应。。。
致命错误:调用未定义的函数重排()
抱歉占用您的时间