我在元数据库中创建了一个自定义字段,其中有一个按钮可以打开媒体管理器并允许选择多个图像。在“打开”事件中,画廊从触发媒体管理器的按钮的“数据ID”属性中检索当前保存选择的图像ID。图像已正确显示并已选定。
我遇到的问题是,在我再次打开媒体管理器并选择新图像后,虽然“数据ID”属性用新选择的图像的新ID更新,但除非我先保存自定义字段,否则库不会用新ID更新以前选择的图像。我做错了什么?我不知道如何“刷新”图像的选择。有人能猜出来吗?
这是我的密码
// Gallery Field Control
var wppf_gallery_ids,
wppf_get_gallery_list,
wppf_get_gallery_field;
GalleryControl = {
// Initializes a new media manager or returns an existing frame.
// @see wp.media.featuredImage.frame()
frame: function() {
if ( this._frame )
return this._frame;
this._frame = wp.media({
title: \'something\',
library: {
type: \'image\'
},
button: {
text: \'something\'
},
multiple: \'toggle\',
});
this._frame.on( \'open\', this.updateFrame ).state(\'library\').on( \'select\', this.select );
return this._frame;
},
select: function() {
var selection = GalleryControl._frame.state().get( "selection" ).toJSON();
var wppf_new_ids = \'\';
// Empty previous images
jQuery(wppf_get_gallery_list).empty();
// Load all stuff up
jQuery(selection).each(function(index) {
jQuery(wppf_get_gallery_list).append(\'<li><input type="hidden" name="\'+ wppf_get_gallery_field +\'[\'+ index +\'][id]" value="\'+ this.id +\'"><input type="hidden" name="\'+ wppf_get_gallery_field +\'[\'+ index +\'][url]" value="\'+ this.url +\'"><img src="\'+ this.url +\'"/></li>\');
wppf_new_ids += this.id + \',\';
});
// Remove last comma
wppf_new_ids = wppf_new_ids.substr(0,wppf_new_ids.length-1);
jQuery(\'#\' + wppf_get_gallery_field).attr(\'data-ids\',wppf_new_ids);
},
updateFrame: function() {
var selection = GalleryControl.frame().state().get(\'selection\');
selection.reset();
// Grab all ids from gallery
var all_ids = wppf_gallery_ids.split(\',\');
// Add to selection
if(all_ids !== \'\') {
jQuery.each(all_ids, function(index, value) {
var attachment = wp.media.attachment( value );
attachment.fetch();
selection.add( attachment ? [ attachment ] : [] );
});
}
},
init: function() {
// Handle media manager
jQuery(\'body\').on(\'click\', \'.wppf-open-gallery\', function(e) {
e.preventDefault();
// Get the gallery id and details
wppf_get_gallery_field = jQuery(this).attr(\'id\');
wppf_gallery_ids = jQuery(this).data(\'ids\');
wppf_get_gallery_list = jQuery(this).data(\'list\');
GalleryControl.frame().open();
});
}
};
// Run Gallery Editor
GalleryControl.init();
打开库的按钮如下所示
<a href="#" id="wppf-gallery-gallery" class="wppf-open-gallery button button-primary wppf-gallery" data-list="#wppf-gallery-thumbs-gallery" data-ids="597,596">Create Gallery</a>
当我打开图库并添加2个新图像时,数据ID属性会用新ID更新,如下所示
<a href="#" id="wppf-gallery-gallery" class="wppf-open-gallery button button-primary wppf-gallery" data-list="#wppf-gallery-thumbs-gallery" data-ids="597,596,381,280">Create Gallery</a>
但是,在我保存帖子之前,媒体管理器不会“预选”这4幅图像。有人知道如何解决这个问题吗?
非常感谢。