2013-06-22 3 views
1

Привет я хочу, чтобы загрузить несколько изображений с wp_insert_post, но только последнее изображение многократного загрузки вставить там другие изображения закачивать но din't прикрепить там ....wp_insert_post мульти загрузка изображений

здесь код

<form action="#" method="post" enctype="multipart/form-data"> <input type="file" name="agp_gallery[]" id="agp_image_files" style="width:90%; margin-left:5px;" multiple> 

здесь функция Multiupload

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

foreach ($_FILES as $file => $array) { 

    agp_process_wooimage($file, $post_id, $result['caption']); 
} 
} 
} 
} 

здесь Главная функция

function agp_process_wooimage($file, $post_id, $caption){ 

    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'); 

    $attachment_id = media_handle_upload($file, $post_id); 

    update_post_meta($post_id, '_product_image_gallery', $attachment_id); 

    $attachment_data = array(
    'ID' => $attachment_id, 
    'post_excerpt' => $caption 
); 

    wp_update_post($attachment_data); 

    return $attachment_id; 

} 

я получить этот код выше здесь: -

http://madebyraygun.com/blog/2012/upload-and-attach-multiple-images-from-the-wordpress-front-end/

и

весь код здесь: -

http://wp.tutsplus.com/tutorials/allow-users-to-submit-images-your-site/

ответ

2

пожалуйста, попробуйте этот

function agp_process_wooimage($file, $post_id){ 

    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'); 

    $attachment_id = media_handle_upload($file, $post_id); 
update_post_meta($post_id, array_push($post_id, '_product_image_gallery', $attachment_id)); 

    return $attachment_id; 

} 
+0

Это не работает – Tsea

1

каждый раз agp_process_wooimage запускает его r заменяет метаданные определенного сообщения id:

post id = 33 метаданных '_product_image_gallery' только последний файл находится там, где файлы были заменены.

Надежда, что помогает ;-)

+0

так, как я могу это исправить – Corlax

+0

в лучшем случае дают мета-данные «_product_image_gallery» нумерацию или сохранить массив вместо одного значения , $ array = get_post_meta ($ post_id, '_product_image_gallery'); $ array = array_push ($ array, $ attachment_id); update_post_meta ($ post_id, $ array); – theode

+0

функция agp_process_wooimage ($ файла, $ post_id, $ setthumb = 'ложь') { \t \t если ($ _FILES [$ файла] [ 'ошибка'] == 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'); $ attachment_id = media_handle_upload ($ file, $ post_id); $ array = get_post_meta ($ post_id, '_product_image_gallery'); $ array = array_push ($ array, $ attachment_id); if ($ setthumb) update_post_meta ($ post_id, $ array); return $ attachment_id; } – Corlax

Смежные вопросы