【问题标题】:add_post_meta create multiple meta_keyadd_post_meta 创建多个 meta_key
【发布时间】:2013-06-26 14:17:38
【问题描述】:

嗨,我创建了一个上传功能,但有一个问题 add_post_meta 创建多个 meta_key 而不是一个 ...

示例:-

meta_key 元值

_product_image_gallery 1
_product_image_gallery 2
_product_image_gallery 3 

应该是……

meta_key 元值

_product_image_gallery 1,2,3


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

        $i=1;

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

        {

            $newupload = agp_process_wooimage($file,$post_id,$i);

            $i++;

        }

    }

}

function agp_process_wooimage($file, $post_id, $count)

{

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

    $post_id = array ();

    $array = array_push($post_id, '_product_image_gallery', $attachment_id);

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

【问题讨论】:

    标签: image wordpress upload


    【解决方案1】:

    对于每个循环,您调用 media_handle_upload() 并将新的 $file 传递给它。我认为这将为每个文件创建一个新的附件 ID。

    然后你在 update_post_meta() 中使用它,如果某些东西不存在,这个函数默认使用 add_post_meta() 。不会,因为您尚未在帖子中添加该特定附件。

    我会看到此脚本按预期工作,您的问题可能缺少某些内容。您是否要求将多个图像添加到单个 meta_value 中?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 2015-04-14
      • 2015-02-12
      相关资源
      最近更新 更多