首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Metabox调用WP图库Uploader/Selector

从Metabox调用WP图库Uploader/Selector
EN

WordPress Development用户
提问于 2014-02-18 15:11:48
回答 1查看 1.3K关注 0票数 1

单击Post/Page上的Add Media按钮时,我可以选择添加Media。选择媒体后,我单击inserted,然后插入图像。但是,还有另一个选项,在左边的侧边栏上。我可以点击创建画廊。图像选择过程是相同的,但是当我单击时,它会转到一个新的框架,它允许我编辑图像的顺序。

第二个窗口就是我要找的东西。我正在从一个metabox调用框架,我已经成功地获得了它,允许我获取单个或多个图像,并将ID保存为字符串,并将缩略图插入预览框中。我找不到任何关于叫画廊画框的东西。

我的当前代码如下:

代码语言:javascript
复制
jQuery('#fg_select').on('click', function(event){

    event.preventDefault();

    // If the media frame already exists, reopen it.
    if ( file_frame ) {
        file_frame.open();
        return;
    }

    // Create the media frame.
    file_frame = wp.media.frame = wp.media({
        title: "Select Images For Gallery",
        button: {text: "Select",},
        library : { type : 'image'},
        multiple: true // Set to true to allow multiple files to be selected
    });

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

    file_frame.on('ready', function() {
        // Here we can add a custom class to our media modal.
        // .media-modal doesn't exists before the frame is
        // completly initialised.
        $( '.media-modal' ).addClass( 'no-sidebar' );
    });

    // When an image is selected, run a callback.
    file_frame.on('select', function() {
        var imageIDArray = [];
        var imageHTML = '';
        var metadataString = '';
        images = file_frame.state().get('selection');
        images.each(function(image) {
            imageIDArray.push(image.attributes.id);
            imageHTML += '<li><button></button><img id="'+image.attributes.id+'" src="'+image.attributes.url+'"></li>';
        });
        metadataString = imageIDArray.join(",");
        if(metadataString){
            jQuery("#fg_metadata").val(metadataString);
            jQuery("#featuredgallerydiv ul").html(imageHTML);
            jQuery('#fg_select').text('Edit Selection');
            jQuery('#fg_removeall').addClass('visible');
        }
    });

    // Finally, open the modal
    file_frame.open();

});

有什么想法吗?

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2014-02-27 13:19:15

想出了问题的答案。

代码语言:javascript
复制
file_frame.on('open', function() {
    var selection = file_frame.state().get('selection');
    var library = file_frame.state('gallery-edit').get('library');
    var ids = jQuery('#fg_perm_metadata').val();
    if (ids) {
        idsArray = ids.split(',');
        idsArray.forEach(function(id) {
            attachment = wp.media.attachment(id);
            attachment.fetch();
            selection.add( attachment ? [ attachment ] : [] );
        });
        file_frame.setState('gallery-edit');
        idsArray.forEach(function(id) {
            attachment = wp.media.attachment(id);
            attachment.fetch();
            library.add( attachment ? [ attachment ] : [] );
        });
    }
});

有关更多细节,请查看:https://stackoverflow.com/questions/21858112/calling-wordpress-gallery-uploader-selector-from-metabox

若要查看运行中的工作代码,请参见:http://wordpress.org/plugins/featured-galleries/

票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/135044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档