我正在使用 wp.media
在插件中选择图像。唯一的问题是我不知道如何加载之前选择的图像。
uploader = wp.media.frames.file_frame = wp.media({
title: 'Select images',
button: {
text: 'Select images'
},
multiple: true,
selection: // current selection = images 173,174,175
});
假设某人之前选择了 3 张图片,然后他们希望再添加一张,这就是框架的样子:
最佳答案
我找到了一个简单的方法来做到这一点。它并不完美,但很简单。
// $input is a hidden input with attachmend IDs, comma-separated
var images = $input.val() || '-1';
uploader = wp.media.gallery.edit('[gallery ids="' + images + '"]');
uploader.state('gallery-edit').on('update', function(selection) {
var images = [];
selection.each(function(attachment) {
images.push(attachment.toJSON().id);
});
$input.val(images.join(','));
});
uploader.open();
关于javascript - 在 WordPress 图片 uploader 中设置选定的图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19638935/