Foreach $ FILES创建帖子如何

时间:2014-02-24 02:06:38

标签: php wordpress

我知道这可能非常简单,我在这里忽略了一些东西。

我在我们网站的前端有一个表单,用户可以通过表单上传图像,并在我们的商店中创建产品。我们正在使用Wordpress和Woocommerce,但我觉得这个问题很普遍,可以在这里询问。

目前上传一张图片时一切正常,当用户上传多张图片时,我该如何使用?

在我改变的形式中 <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 $ FILES中不起作用?

$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 );
}

简而言之,我只需要一种方法让上面的代码为每个上传的文件创建帖子,而不是像目前那样只上传一个文件。我所有人都没有想法

1 个答案:

答案 0 :(得分:1)

知道了,所以我将文件输入的名称从缩略图更改为缩略图[]。然后我将上面的粘贴包装在一个函数中,并将此部分添加到文件的顶部,它完美地运行:

if ( $_FILES ) {
    $files = $_FILES['thumbnail'];
        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("" => $file);

              foreach ($_FILES as $file => $array) {
                      //call my function
                  create_var_product($file, $post_id);
              }
          }
    }
}