簡體   English   中英

WordPress:使用中繼器圖像字段將圖像上傳到ACF

[英]WordPress: Upload images to ACF using repeater image field

我的HTML表單允許用戶選擇任意數量的圖像以上傳到自定義帖子類型。 我在ACF中創建了一個“圖像”對象的轉發器字段,稱為“圖像”。

如何將上傳的圖像保存到該轉發器字段?

這是我目前擁有的:

$files = $_FILES['file'];
$image_number = 1;
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]
    );
    $uploaded_file = wp_handle_upload($file, array('test_form' => FALSE));
    update_sub_field( array('images', $image_number++, 'image'), $uploaded_file, $post_id);
  }
}

但是圖像不會保存。 任何幫助表示贊賞,謝謝!

據我所知,您對update_field函數的使用尤其是其簽名是錯誤的。 您混合了參數。 在這里看看: Wordpress ACF:如何通過自定義代碼(PHP)將行添加到附加到用戶的轉發器字段中

請嘗試以下代碼。 它為我工作。 盡管這是一個過時的帖子,但將來可能會對其他人有所幫助。

$attachment = array(
    'guid'           => $upload_dir . '/' . basename( $filename ),
    'post_mime_type' => $filetype['type']
);
$attachment_id = wp_insert_attachment($attachment, $filename, $parent_post_id);

$row = array(
        'name' => "Hello",
        'logo' => $attachment_id,  // this is where you put post id of your image
        'website' => "http://hello.com"
    );

add_row( "repeater_field", $row, $blog_post_id);

我還注意到,只有當您已經至少有一行時,add_row才起作用。 但是,如果要添加新行,請使用update_field。 在這里看看: ACF Pro中的add_row沒有保存轉發器值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM