Как активировать медиа-галерею в кастомном медиа-загрузчике WordPress

Я делаю пользовательский медиа-загрузчик, и он отлично работает в метаполе. В моем медиа-загрузчике нет опции галереи. Но теперь я хочу дать вариант для галереи на нем. Я много ищу и следую их шагам, но безуспешно.

Вот мой код, который прекрасно работает без опции галереи:

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();
});

А вот мой код для галереи:

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();
});

Сначала я хочу опцию для галереи, а во-вторых, когда я создаю галерею, ее короткий код появится в метаполе, а затем я сохраняю его в БД.

Пожалуйста, дайте любое руководство, которое поможет мне.

0

Решение

Я выяснил это путем дополнительного поиска и нашел некоторые ссылки, проверил их. Теперь опция галереи активна в моем медиа-загрузчике.

Вот мой код сейчас:
Просто добавьте еще три параметра

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

в этой строке meta_image_frame_gallery = wp.media.frames.wp_media_frame = wp.media( {

Полный код:

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
} );

И теперь он работает отлично

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector