无法在WordPress中保存多个图像

时间:2017-05-05 作者:112233

我正在创建元框,将图片库附加到帖子中。但我保存后只得到一张图像。请更正下面的回调函数:

function save_custom_meta_data($id) {

    /* --- security verification --- */
    if(!wp_verify_nonce($_POST[\'wp_custom_attachment_nonce\'], plugin_basename(__FILE__))) {
      return $id;
    } // end if

    if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
      return $id;
    } // end if

    if(\'page\' == $_POST[\'post_type\']) {
      if(!current_user_can(\'edit_page\', $id)) {
        return $id;
      } // end if
    } else {
        if(!current_user_can(\'edit_page\', $id)) {
            return $id;
        } // end if
    } // end if
    /* - end security verification - */

    // Make sure the file array isn\'t empty
    foreach ($_FILES[\'wp_custom_attachment\']["tmp_name"] as $key => $value) {

        if(!empty($_FILES[\'wp_custom_attachment\'][\'name\'][$key])) {

            // Setup the array of supported file types. In this case, it\'s just PDF.
            $supported_types = array(\'application/pdf\', \'image/jpeg\', \'image/png\', \'image/jpg\', \'image/gif\');

            // Get the file type of the upload
            $arr_file_type = wp_check_filetype(basename($_FILES[\'wp_custom_attachment\'][\'name\'][$key]));
            $uploaded_type = $arr_file_type[\'type\'];

            // Check if the type is supported. If not, throw an error.
            if(in_array($uploaded_type, $supported_types)) {

                // Use the WordPress API to upload the file
                $upload = wp_upload_bits($_FILES[\'wp_custom_attachment\'][\'name\'][$key], null, file_get_contents($_FILES[\'wp_custom_attachment\'][\'tmp_name\'][$key]));

                if(isset($upload[\'error\']) && $upload[\'error\'] != 0) {
                    wp_die(\'There was an error uploading your file. The error is: \' . $upload[\'error\']);
                } else {
$upload提供所选多个图像的正确数组值。但我不确定add\\u post\\u meta和update\\u post\\u meta是否保存了多个图像。

                    add_post_meta($id, \'wp_custom_attachment\', $upload);

                    update_post_meta($id, \'wp_custom_attachment\', $upload);     
                } // end if/else

            } else {

                wp_die("The file type that you\'ve uploaded is not of supported type.");
            } // end if/else

        } // end if
    }


} // end save_custom_meta_data
add_action(\'save_post\', \'save_custom_meta_data\');
我正在获取如下图像:

$file = get_post_meta(get_the_ID(), \'wp_custom_attachment\', false); 
var_dump($file);//给我的是错误的相同图像数组。

我不知道如何检查是否保存了所有图像?它们是否被覆盖,这就是为什么我在使用get_post_meta?

1 个回复
SO网友:Nate

您正在更新相同的post_meta 在每个回路上。因此,您可能只保存上次上载的图像。试试这样(简化):

foreach ($_FILES[\'wp_custom_attachment\']["tmp_name"] as $key => $value) {
  $post_meta = get_post_meta($id, \'wp_custom_attachment\', $upload);

  if( $post_meta ) {
    $post_meta .= \', \' . $upload;
    update_post_meta($id, \'wp_custom_attachment\', $post_meta);
  }

  else {
    add_post_meta($id, \'wp_custom_attachment\', $upload);
  }
}
首先,我检查post_meta 存在。如果没有,我将使用$upload创建它。如果确实存在,我添加新的$upload 到中的现有值$post_meta. 当需要获取所有值时,可以使用explode().

$images = explode( \',\', $post_meta );
  foreach( $images as $img ) {
    // bla...
  }
另一种方法是为每次上载创建不同的唯一密钥并在其上循环。

foreach ($_FILES[\'wp_custom_attachment\']["tmp_name"] as $key => $value) {
  $post_meta = get_post_meta($id, \'wp_custom_attachment_1\', $upload);

  if( !$post_meta ) {
    add_post_meta($id, \'wp_custom_attachment_1\', $upload);
  }

  else {
    $counter = 1;

    while( $flag !== true ) {
      $post_meta = get_post_meta($id, \'wp_custom_attachment_\' . $counter, $upload);

      if( $post_meta ) {
        continue;
      }

      else {
        add_post_meta($id, \'wp_custom_attachment_\' . $counter, $upload);
      }

      $counter++;
    }
  }
}
代码是原始的和简化的。

相关推荐

仅为主页显示Metabox

我将尝试使用设置字段将metabox添加到主页,但出现问题,请帮助我。删除时,metabox不会显示在页面编辑器中if statement 它显示在所有页面上。add_action(\'add_meta_boxes\', \'metabox_homepage_videos\'); function metabox_homepage_videos($post) { if (is_front_page()): add_meta_box(\'metabox