如何从媒体库中获取输入框中的图片url?

时间:2014-04-27 作者:тнє Sufi

我使用wordpress media uploader添加了一个post meta来上传图像。看起来这是一个javascript工作。我不擅长javascript。我从here:

以下是js代码:

jQuery(document).ready(function($){

    // Instantiates the variable that holds the media library frame.
    var meta_image_frame;

    // Runs when the image button is clicked.
    $(\'#meta-image-button\').click(function(e){

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

        // If the frame already exists, re-open it.
        if ( meta_image_frame ) {
            wp.media.editor.open();
            return;
        }

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

        // Runs when an image is selected.
        meta_image_frame.on(\'select\', function(){

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

            // Sends the attachment URL to our custom image input field.
            $(\'#meta-image\').val(media_attachment.url);
        });

        // Opens the media library frame.
        wp.media.editor.open();
    });
});
我有一个id为的输入框#meta-image. 但它不会在那里返回img url。相反,它将图像url放在主post编辑器中。你知道我该怎么做吗?

1 个回复
最合适的回答,由SO网友:тнє Sufi 整理而成

我通过改变wp.media.editor.open()meta_image_frame.open().

现在一切正常。但我不确定之前的错误是什么。

结束