如何使用wp_ins_post上传多张图片

时间:2018-11-28 作者:Tejas Gajjar

Im使用wp\\U insert\\U post在woocommerces网站的前端添加产品

我的当前代码将上载所有图像,但只有最后一个图像将位于产品库图像中

这是我的密码;

功能。php

function my_handle_attachment( $file_handler, $post_id, $set_thu=false) {
  // 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 );


  if ( is_numeric( $attach_id ) ) {

    update_post_meta( $post_id, \'_product_image_gallery\', $attach_id );

  }
  return $attach_id;  
}
前端

if ( $_FILES ) {
    $files = $_FILES[\'upload_attachment\'];
    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("upload_attachment" => $file);
            foreach ($_FILES as $file => $array) {
                $newupload = my_handle_attachment($file,$post_id);
                 update_post_meta($post_id,  array_push($post_id, \'_product_image_gallery\',$newupload));
            }
        }
    }
}


<input type="file" name="upload_attachment[]" multiple="multiple"  />
这个代码有什么问题吗?

2 个回复
最合适的回答,由SO网友:Pratik bhatt 整理而成

你好Tejas你可以修改下面的代码,如果它对你有用,请告诉我。

    foreach ($files[\'name\'] as $key => $value) { 

$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 ("files" => $file); 
foreach ($_FILES as $file => $array) { 
$newupload = my_handle_attachment($file,$post_id); 

}
array_push($gallery,$newupload);

} 
update_post_meta($post_id,\'_product_image_gallery\', implode(\',\',$gallery));

SO网友:vikrant zilpe

you can clean out the title attributes yourself by adding the following to your functions.php file:

function remove_title_attributes($input) {
return preg_replace(\'/\\s*title\\s*=\\s*(["\\\']).*?\\1/\', \'\', $input);
 }
 add_filter( \'wp_list_pages\', \'remove_title_attributes\' );
结束

相关推荐

Override img class in gallery

我使用Wordpress for AngularJS作为后端(wp json插件),实际上我为主题制作的结构是这样的myApptheme -style.css //nothing special here just inits the theme -index.php -header.php -functions.php lib //here some overrides php --gallery.php app //h