jquery - 在 WordPress 媒体选择器中获取多个图像

标签 jquery wordpress

我有以下 jQuery,它将打开 wordpress 媒体选择器并返回所选媒体文件的 url。

我已将 multiple 设置为 true,这样我就可以选择多个图像。

我的问题是如何获取第二张图片的网址。我似乎只能得到第一个。

额外问题 - 有没有办法将多重选择限制为仅 2 个图像?

jQuery(function($) {
        $(document).ready(function(){
                $('#insert-my-media').click(open_media_window);
            });

            function open_media_window() {
                if (this.window === undefined) {
                    this.window = wp.media({
                            title: 'Select a Before and After image',
                            library: {type: 'image'},
                            multiple: true,
                            button: {text: 'Insert'}
                        });

                    var self = this; // Needed to retrieve our variable in the anonymous function below
                    this.window.on('select', function() {
                            var first = self.window.state().get('selection').first().toJSON();
                            wp.media.editor.insert('[banda before="' + first.url + ' after="' + second.url + '"]');
                        });
                }

                this.window.open();
                return false;
            }
    });

最佳答案

最好的方法是创建媒体编辑器的 2 个实例, 并使用“multiple: false”。

注意 $(document).ready 是无用的,因为通过使用语法 jQuery(my_function)my_function 在文档就绪时执行。

您可能需要这样的东西:

jQuery(function ($) {
    var mediaPopup;
    var placeholders = {
        before: "@BandaBefore@",
        after: "@BandaAfter@"
    };
    var attrs = {
        before: "before=",
        after: "after="
    };
    var editorText = "[banda before='" + placeholders.before +
         "' after='" + placeholders.after + "']";
    $("#choseBeforeMedia").on("click", function () {
        openMediaPopup("before");
    });
    $("#choseAfterMedia").on("click", function () {
        openMediaPopup("after");
    });

    var currentValues = {
        before: function () {
            var idx1 = editorText.indexOf(attrs.before) + attrs.before.length + 1;
            var idx2 = editorText.indexOf(attrs.after) - 2;
            return editorText.substring(idx1, idx2);
        },
        after: function () {
            var idx1 = editorText.indexOf(attrs.after) + attrs.after.length + 1;
            var tmp = editorText.substring(idx1);
            var idx2 = tmp.indexOf("'");
            return tmp.substring(0, idx2);
        }
    };

    function openMediaPopup(label) {
        if (mediaPopup === undefined) {
            mediaPopup = wp.media({
                title: "Select the " + label + " image",
                library: {type: "image"},
                multiple: true,
                button: {text: "Insert"}
            });

            mediaPopup.on("select", function () {
                var img = self.window.state().get("selection").toJSON();
                if (editorText.indexOf(placeholders[label]) > -1) {
                    editorText = editorText.replace(placeholders[label], img.url);
                }
                else {
                    editorText = editorText.replace(currentValues[label](), img.url);
                }
                wp.media.editor.insert(editorText);
           });
       }

       mediaPopup.open();

    }
});

关于jquery - 在 WordPress 媒体选择器中获取多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42321054/

相关文章:

javascript - 如何用一个 .toggle() 切换多个元素

javascript - 如何使用 jquery ajax 和 Php 在单击按钮时加载页面?

wordpress - 在独立脚本中使用 WPDB?

wordpress - 如何从post_meta获取post_id

php - 如何使用 CMS 中动态输入的数据在 Highcharts 中构建柱形图

javascript - jQuery 选择器中的括号

jquery - 奇怪的 JQuery $.post() 行为

javascript - 如何在html中使用onload事件来调用js文件中的函数

php - 在 Woocommerce 存档页面中按原始正确设置元素数

php - 未使用 WordPress 子主题 css