Wordpress自定义元框多个图像上传

时间:2016-04-30 23:05:13

标签: javascript php jquery wordpress

我正在使用自定义帖子类型,其中添加了自定义元框。在自定义元框中,我正在尝试为多个图像添加媒体上传器。有了这个上传器,我想在一个数组中存储多个图像ID。问题是我只设法保存一个ID,即使我选择了多个图像。

我真的希望有人可以帮助我。

我的JS:

    jQuery(document).ready(function(){
  var addButton = document.getElementById('last-opp-bilde');
  var deleteButton = document.getElementById('fjern-bilde');
  var img = document.getElementById('image-tag');
  var hidden = document.getElementById('img-hidden-field');
  var imageUploader = wp.media({
    title: 'Velg bilde',
    button: {
      text: 'Bruk dette bildet'
    },
    multiple: true
  });

  addButton.addEventListener( 'click', function() {
    if (imageUploader) {
      imageUploader.open();
    }
  });

  imageUploader.on( 'select', function() {
    var attachment = imageUploader.state().get('selection').first().toJSON();
    img.setAttribute( 'src', attachment.url);
    img.setAttribute( 'style', 'width: 50%;');
    hidden.setAttribute( 'value', JSON.stringify( [ { id: attachment.id, url: attachment.url}]));
  });

  deleteButton.addEventListener( 'click', function(){
    img.removeAttribute( 'src' );
    hidden.removeAttribute( 'value' );
    img.removeAttribute( 'style' );
  });

  window.addEventListener( 'DOMContentLoaded', function(){
    img.setAttribute( 'src', imageUploads.imageData.src);
    img.setAttribute( 'style', 'width: 50%;');
    hidden.setAttribute('value', JSON.stringify( [imageUploads.imageData]));
  });
});

1 个答案:

答案 0 :(得分:1)

我实际上设法解决了这个问题。这是为我做的工作的JS。

    jQuery(document).ready(function(){
  var addButton = document.getElementById('last-opp-bilde');
  //var deleteButton = document.getElementById('fjern-bilde');
  var img = document.getElementById('image-tag');
  var hidden = document.getElementById('img-hidden-field');
  var imageUploader = wp.media({
    title: 'Velg bilde',
    button: {
      text: 'Velg bilde(r)'
    },
    multiple: 'add'
  });

  addButton.addEventListener( 'click', function() {
    if (imageUploader) {
      imageUploader.open();
    }
  });

  imageUploader.on('open',function() {
    var selection = imageUploader.state().get('selection');
    ids = jQuery('#img-hidden-field-selected').val().split(',');
      ids.forEach(function(id) {
    attachment = wp.media.attachment(id);
    attachment.fetch();
    selection.add( attachment ? [ attachment ] : [] );
  });
  });

  imageUploader.on( 'close', function() {
    //var attachment = imageUploader.state().get('selection').first().toJSON();
    var attachment = imageUploader.state().get('selection');
    var ids = attachment.map( function (attachment) {
        return attachment.id;
    });
    hidden.setAttribute( 'value', ids.join(',') );
  });

  imageUploader.on( 'select', function() {
    //var attachment = imageUploader.state().get('selection').first().toJSON();
    var attachment = imageUploader.state().get('selection');
    var ids = attachment.map( function (attachment) {
        return attachment.id;
    });
    hidden.setAttribute( 'value', ids.join(',') );
  });
});