我知道这可能非常简单,我忽略了这里的一些东西。
我在我们网站的前端有一个表单,用户可以通过表单上传图像,然后在我们的店铺中创建产品。
目前,上传一幅图像时一切正常,当用户上传多幅图像时,我该如何做到这一点?
以我改变的形式<input type="file" name="thumbnail" id="thumbnail" />
到<input type="file" name="thumbnail" id="thumbnail" multiple />
我想用这样的东西包装下面的内容会有用:
if ($_FILES) {
foreach ($_FILES as $file => $array) {
// do necessary code
}
}
但事实并非如此。
这是我创建这篇文章的资料。我不知道为什么将其包装在foreach$文件中不起作用?
$current_user = wp_get_current_user();
if(isset($_POST[\'submitted\']) && isset($_POST[\'post_nonce_field\']) && wp_verify_nonce($_POST[\'post_nonce_field\'], \'post_nonce\')) {
$postTitle = trim($_POST[\'postTitle\']);
if($post_id)
{
wp_redirect(home_url());
exit;
}
//random sku
$skuu = rand();
$new_post = array(
\'post_title\' => $skuu,
\'post_content\' => \'\', //add anything here like link to vendor or whatever
\'post_status\' => \'publish\',
\'post_type\' => \'product\'
);
//insert and update post
$post_id = wp_insert_post($new_post);
update_post_meta($post_id, \'_sku\', $skuu );
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post_id);
}
}
//find available attributes
$avail_attributes = Array(
\'high-resolution\',
\'medium-resolution\',
\'low-resolution\'
);
//set terms (variations and attributes)
wp_set_object_terms ($post_id, \'variable\', \'product_type\');
wp_set_object_terms( $post_id, $avail_attributes, \'pa_resolution\' );
//Categories
wp_set_object_terms( $post_id, array(esc_attr(strip_tags($_POST[\'postCat\'])), esc_attr(strip_tags($_POST[\'raceCat\']))), \'product_cat\' );
// set variable data
$thedata = Array(\'pa_resolution\'=>Array(
\'name\'=>\'pa_resolution\',
\'value\'=>\'\',
\'is_visible\' => \'1\',
\'is_variation\' => \'1\',
\'is_taxonomy\' => \'1\'
));
update_post_meta( $post_id,\'_product_attributes\',$thedata);
update_post_meta( $post_id, \'_visibility\', \'visible\' );
update_post_meta( $post_id, \'_stock_status\', \'instock\');
//update_post_meta( $post_id, \'_product_vendors_commission\', \'25\');
//insert variations post_type
$i=1;
while ($i<=3) {
$my_post = array(
\'post_title\' => \'Variation #\' . $i . \' of \' . esc_attr(strip_tags($_POST[\'postTitle\'])),
\'post_name\' => \'product-\' . $post_id . \'-variation-\' . $i,
\'post_status\' => \'publish\',
\'post_parent\' => $post_id,
\'post_type\' => \'product_variation\',
\'guid\' => home_url() . \'/?product_variation=product-\' . $post_id . \'-variation-\' . $i,
\'post_author\' => $vendor_data->term_id
);
// Insert the post into the database
wp_insert_post( $my_post );
$variable_id = $post_id + 2;
$variable_two = $variable_id + 1;
$variable_three = $variable_two + 1;
//get user prices
global $current_user;
$low_price = get_user_meta($current_user->ID, \'_low_price\', true);
$medium_price = get_user_meta($current_user->ID, \'_medium_price\', true);
$high_price = get_user_meta($current_user->ID, \'_high_price\', true);
//downloadable file paths
$download_id = get_post_thumbnail_id( $post_id );
$lowRes_src = wp_get_attachment_image_src( $download_id, \'low-resolution\' );
$medRes_src = wp_get_attachment_image_src( $download_id, \'medium-resolution\' );
$highRes_src = wp_get_attachment_image_src( $download_id, \'high-resolution\' );
$low_downloadable_images = array();
$medium_downloadable_images = array();
$high_downloadable_images = array();
foreach ($lowRes_src as $lowRes_url) {
$lowRes_url = $lowRes_src[0];
$low_downloadable_images[md5( $lowRes_url )] = $lowRes_url;
}
foreach ($medRes_src as $medRes_url) {
$medRes_url = $medRes_src[0];
$medium_downloadable_images[md5( $medRes_url )] = $medRes_url;
}
foreach ($highRes_src as $highRes_url) {
$highRes_url = $highRes_src[0];
$high_downloadable_images[md5( $highRes_url )] = $highRes_url;
}
//echo $rand;
update_post_meta( $variable_id, \'attribute_pa_resolution\', \'high-resolution\');
update_post_meta( $variable_id, \'_price\', $high_price );
update_post_meta( $variable_id, \'_regular_price\', $high_price);
update_post_meta( $variable_id, \'_virtual\', \'yes\');
update_post_meta( $variable_id, \'_downloadable\', \'yes\');
update_post_meta( $variable_id, \'_file_paths\', $high_downloadable_images);
update_post_meta( $variable_two, \'attribute_pa_resolution\', \'medium-resolution\');
update_post_meta( $variable_two, \'_price\', $medium_price );
update_post_meta( $variable_two, \'_regular_price\', $medium_price);
update_post_meta( $variable_two, \'_virtual\', \'yes\');
update_post_meta( $variable_two, \'_downloadable\', \'yes\');
update_post_meta( $variable_two, \'_file_paths\', $medium_downloadable_images);
update_post_meta( $variable_three, \'attribute_pa_resolution\', \'low-resolution\');
update_post_meta( $variable_three, \'_price\', $low_price );
update_post_meta( $variable_three, \'_regular_price\', $low_price);
update_post_meta( $variable_three, \'_virtual\', \'yes\');
update_post_meta( $variable_three, \'_downloadable\', \'yes\');
update_post_meta( $variable_three, \'_file_paths\', $low_downloadable_images);
$i++;
}
}
//attach vendor to product
$vendor = get_user_vendor();
if( isset( $vendor->slug ) && strlen( $vendor->slug ) > 0 ) {
wp_set_object_terms( $post_id, $vendor->slug, \'shop_vendor\', false );
}
简而言之,我只需要一种方法,让上面的代码为每个上传的文件创建帖子,而不是像现在这样只上传一个文件。。我没有主意了