doubei3312 2017-02-20 07:16
浏览 45
已采纳

如何在自定义媒体上传器WordPress中激活媒体库

I make custom media up loader and its working perfectly on a meta field. In my media up loader there is no gallery option. But now I want to give an option for gallery on it. I search a lot and follow their steps but not succeed.

Here is my Code that working perfectly without gallery option:

var meta_image_frame_gallery;

    // Runs when the image button is clicked.
    jQuery('#additional_image_1').click(function(e){

        // Prevents the default action from occuring.
        e.preventDefault();

        // If the frame already exists, re-open it.
        if ( meta_image_frame_gallery ) {
            meta_image_frame_gallery.open();
            return;
        }

        // Sets up the media library frame
        meta_image_frame_gallery = wp.media.frames.meta_image_frame_gallery = wp.media({
            title: 'Upload Image',
            button: { text:  'Upload Image' },
            library: { type: 'image' },
        });

         Runs when an image is selected.
        meta_image_frame_gallery.on('select', function(){

            // Grabs the attachment selection and creates a JSON representation of the model.
            var media_attachment = meta_image_frame_gallery.state().get('selection').first().toJSON();

            // Sends the attachment URL to our custom image input field.
            jQuery('#itv_additional_image_1').val(media_attachment.url);
        });


        // Opens the media library frame.
        meta_image_frame_gallery.open();
    });

And here is my code for gallery option:

var meta_image_frame_gallery;

    // Runs when the image button is clicked.
    jQuery('#additional_image_1').click(function(e){

        // Prevents the default action from occuring.
        e.preventDefault();

        // If the frame already exists, re-open it.
        if ( meta_image_frame_gallery ) {
            meta_image_frame_gallery.open();
            return;
        }

        // Sets up the media library frame
        meta_image_frame_gallery = wp.media.frames.meta_image_frame_gallery = wp.media({
            title: 'Upload Image',
            button: { text:  'Upload Image' },
            multiple: true,
            library: { type: 'image' },
        });

        // Runs when an image is selected.
        //meta_image_frame_gallery.on('select', function(){
        //
        //    // Grabs the attachment selection and creates a JSON representation of the model.
        //    var media_attachment = meta_image_frame_gallery.state().get('selection').first().toJSON();
        //
        //    // Sends the attachment URL to our custom image input field.
        //    jQuery('#itv_additional_image_1').val(media_attachment.url);
        //});

        // When an image is selected, run a callback.
        meta_image_frame_gallery.on( 'select', function() {

            var selection = meta_image_frame_gallery.state().get('selection');

            selection.map( function( attachment ) {

                attachment = attachment.toJSON();

                // Do something with attachment.id and/or attachment.url here
                jQuery('#itv_additional_image_1').val(selection.url);
            });
        });

        // Opens the media library frame.
        meta_image_frame_gallery.open();
    });

First I want an option for gallery and second when I create a gallery, its short-code will appear on meta field and then i save it in db.

Please any give any guidance that help me out.

  • 写回答

1条回答 默认 最新

  • duanhuo7441 2017-02-20 11:57
    关注

    I figure it by more searching and found some links check them. Now gallery option is active in my media up loader.

    Here is my Code now: Just add three more params

    frame: "post", state: 'gallery-library', multiple: true

    in this line meta_image_frame_gallery = wp.media.frames.wp_media_frame = wp.media( {

    Complete code:

    meta_image_frame_gallery = wp.media.frames.wp_media_frame = wp.media( {
                title: 'My Gallery',
                frame: "post",
                state: 'gallery-library',
                library: {
                    type: 'image'
                },
                multiple: true
            } );
    

    And now its working perfectly

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?