我有一个问题,提交自定义后,从前端与图像。我有一个带有帖子标题、描述和图片的表单,应该是帖子的特色图片。因此,当用户提交帖子时,它实际上创建了标题和描述,但没有图像,即使在媒体库中,图像也不会出现。Here is my form:
<form id="_themename-advert-create-form" method="post" enctype="multipart/form-data">
<?php wp_nonce_field(\'_themename_submit_advert_action\',\'__themename_submit_advert_nonce\'); ?>
<input type="hidden" name="_themename-advert-create-check" value="1" />
<label for="_themename-advert-create-title">Title</label>
<input id="_themename-advert-create-title" type="text" name="_themename-advert-create-title" />
<label for="_themename-advert-create-content">Sample Content</label>
<textarea id="_themename-advert-create-content" rows="8" name="_themename-advert-create-content"></textarea>
<label for="_themename-advert-create-image">Upload Post Image</label>
<input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image" />
<input type="submit" id="_themename-advert-create-submit" value="SUBMIT" name="_themename-advert-create-submit" />
</form>
Here is my function that creates custom post:
add_action(\'init\', \'_themename_create_new_post\');
function _themename_create_new_post(){
if(\'POST\' == $_SERVER[\'REQUEST_METHOD\'] && isset($_POST[\'_themename-advert-create-check\']) && wp_verify_nonce( $_POST[\'__themename_submit_advert_nonce\'], \'_themename_submit_advert_action\')) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$new_post_title = sanitize_text_field($_POST[\'_themename-advert-create-title\']);
$new_post_content = sanitize_textarea_field($_POST[\'_themename-advert-create-content\']);
$new_post = array();
$new_post[\'post_author\'] = $user_id;
$new_post[\'post_title\'] = $new_post_title;
$new_post[\'post_content\'] = $new_post_content;
$new_post[\'post_status\'] = \'pending\';
$new_post[\'post_name\'] = \'pending\';
$new_post[\'post_type\'] = \'jana_usa\';
$post_success = wp_insert_post($new_post);
if ( $_FILES ) {
$files = $_FILES["_themename-advert-create-image"];
foreach ($files[\'name\'] as $key => $value) {
if ($files[\'name\'][$key]) {
$file = array(
\'name\' => $files[\'name\'][$key],
\'type\' => $files[\'type\'][$key],
\'tmp_name\' => $files[\'tmp_name\'][$key],
\'error\' => $files[\'error\'][$key],
\'size\' => $files[\'size\'][$key]
);
$_FILES = array ("_themename-advert-create-image" => $file);
foreach ($_FILES as $file => $array) {
// $newupload = frontend_handle_attachment( $file, $post_success );
if ($_FILES[$file][\'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, $post_success );
// Set featured image
set_post_thumbnail($post_success, $attach_id);
}
}
}
}
}
}
提交后,我出现了一个错误
Warning: Invalid argument supplied for foreach() in ... on line 43
. 该行:
foreach ($files[\'name\'] as $key => $value) { ...
我找不到解决办法。我做错了什么?
最合适的回答,由SO网友:AkForDev 整理而成
问题就在这里:
<input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image" />
它应该是:
<input type="file" id="_themename-advert-create-image" name="_themename-advert-create-image[]" />
我仍然不明白为什么要在我的名字中添加[],但现在一切都正常了。