2013-03-26 59 views
10

Tôi có biểu mẫu có trường tải lên ($_FILES['watch_photo']).cách tải hình ảnh lên ACF bằng update_field trên wordpress

Tôi đã xem xét và kết hợp chức năng này lại với nhau.

Về cơ bản, nó lấy tất cả thông tin liên quan để nó có thể sử dụng lại trong tương lai, nó sẽ trả về một mảng $ pid và URL của tệp, khi nó được thực hiện.

Vấn đề là ACF vẫn chưa cung cấp nhiều thông tin làm thế nào để thêm hình ảnh vào nó các lĩnh vực sử dụng update_field()http://www.advancedcustomfields.com/resources/functions/update_field/

function my_update_attachment($f,$pid,$t='',$c='') { 
    wp_update_attachment_metadata($pid, $f); 
    if(!empty($_FILES[$f]['name'])) { //New upload 
    require_once(ABSPATH . 'wp-admin/includes/file.php'); 

    $override['action'] = 'editpost'; 
    $file = wp_handle_upload($_FILES[$f], $override); 

    if (isset($file['error'])) { 
     return new WP_Error('upload_error', $file['error']); 
    } 

    $file_type = wp_check_filetype($_FILES[$f]['name'], array(
     'jpg|jpeg' => 'image/jpeg', 
     'gif' => 'image/gif', 
     'png' => 'image/png', 
    )); 
    if ($file_type['type']) { 
     $name_parts = pathinfo($file['file']); 
     $name = $file['filename']; 
     $type = $file['type']; 
     $title = $t ? $t : $name; 
     $content = $c; 

     $attachment = array(
     'post_title' => $title, 
     'post_type' => 'attachment', 
     'post_content' => $content, 
     'post_parent' => $pid, 
     'post_mime_type' => $type, 
     'guid' => $file['url'], 
    ); 

     foreach(get_intermediate_image_sizes() as $s) { 
     $sizes[$s] = array('width' => '', 'height' => '', 'crop' => true); 
     $sizes[$s]['width'] = get_option("{$s}_size_w"); // For default sizes set in options 
     $sizes[$s]['height'] = get_option("{$s}_size_h"); // For default sizes set in options 
     $sizes[$s]['crop'] = get_option("{$s}_crop"); // For default sizes set in options 
     } 

     $sizes = apply_filters('intermediate_image_sizes_advanced', $sizes); 

     foreach($sizes as $size => $size_data) { 
     $resized = image_make_intermediate_size($file['file'], $size_data['width'], $size_data['height'], $size_data['crop']); 
     if ($resized) 
      $metadata['sizes'][$size] = $resized; 
     } 

     $attach_id = wp_insert_attachment($attachment, $file['file'] /*, $pid - for post_thumbnails*/); 

     if (!is_wp_error($id)) { 
     $attach_meta = wp_generate_attachment_metadata($attach_id, $file['file']); 
     wp_update_attachment_metadata($attach_id, $attach_meta); 
     } 

     return array(
     'pid' =>$pid, 
     'url' =>$file['url'] 
    ); 
     // update_post_meta($pid, 'a_image', $file['url']); 
    } 
    } 
} 

và sau đó tôi đã sử dụng đoạn mã sau:

  • để tạo ra một bưu chính sau đó
  • tải lên sau đó sử dụng my_update_attachment để xử lý hình ảnh
  • và cuối cùng cập nhật dvanced tùy chỉnh lĩnh vực

Mã:

$args= array( 'post_type' => 'watches',  'post_status' => 'publish' ); 

$watch = wp_insert_post($args); 
$att = my_update_attachment('watch_image',$watch); 
update_field('field_512e085a9372a',$att['url'],$watch); 

tôi đang thiếu gì? Mọi sự trợ giúp sẽ rất được trân trọng.

+0

Bạn đã sử dụng phần update_field() như thế nào? là trong một chức năng? –

Trả lời

16

Tôi gặp vấn đề tương tự, bạn gần như đã làm đúng, điều này cũng giúp ích cho tôi.

nhìn ACF và update_field chấp nhận ID tập tin đính kèm như phản đối các sự, pid hoặc url

Bạn có thể thay thế các mảng trở lại:

return array(
    'pid' =>$pid, 
    'url' =>$file['url'] 
); 

Với các mảng sau:

return array(
    'pid' =>$pid, 
    'url' =>$file['url'], 
    'file'=>$file, 
    'attach_id'=>$attach_id 
); 

và sau đó thay đổi

update_field('field_512e085a9372a',$att['url'],$watch); 

như sau

update_field('field_512e085a9372a',$att['attach_id'],$watch); 

Tôi cũng đã viết blog về nó ở đây http://www.jqui.net/wordpress/acf-upload-image-made-easy/

+0

lưu ngày của tôi bruh !! –

-1

Kiểm tra plugin này:

https://wordpress.org/plugins/acf-frontend-display/

  1. Tạo mẫu với lĩnh vực frontendUploader ACF (Plugin gia hạn)
  2. Thêm biểu mẫu ACF của bạn vào bài đăng và bằng bảng điều khiển sử dụng bảng điều khiển quản trị để xem ở mặt trước
+0

Plugin của bạn dường như không hoạt động với ACF V5. Vì vậy, nó không thực sự là một giải pháp. –

Các vấn đề liên quan