如何在meta字段中创建画廊时获取wp画廊的简码 [英] How to get shortcode of wp gallery on creating gallery in meta field
本文介绍了如何在meta字段中创建画廊时获取wp画廊的简码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在meta字段上创建一个图库媒体加载程序.它的工作完美.
I create a gallery media up loader on a meta field. Its working perfectly.
当我单击Browse
时,打开了图库媒体加载程序,我选择了图像,然后单击了Insert Gallery
,但是在输入meta字段中没有得到图库的简码.
When I click on Browse
, a gallery media up loader is open, I select images and then click on Insert Gallery
but I didn't get a shortcode of a gallery in input meta field.
这是我从互联网上获得的代码:
Here is my code that I get from internet:
var meta_image_frame_gallery;
$( '#additional_image_1' ).click(function( event ) {
event.preventDefault();
//var images = $( '#itv_additional_image_1' ).val();
//var gallery_state = images ? 'gallery-edit' : 'gallery-library';
if ( meta_image_frame_gallery ) {
meta_image_frame_gallery.open();
return;
}
// create new media frame
// You have to create new frame every time to control the Library state as well as selected images
meta_image_frame_gallery = wp.media.frames.wp_media_frame = wp.media( {
title: 'My Gallery', // it has no effect but I really want to change the title
frame: "post",
//toolbar: 'main-gallery',
state: 'gallery-library',
library: {
type: 'image'
},
multiple: true
} );
} );
这是我的html代码:
And here is my html code:
<input id="itv_additional_image_1" class="input-text" name="itv_additional_image_1" placeholder="" type="text">
<input id="additional_image_1" name="additional_image_1" value="Browse" type="button">
我在jquery方面非常虚弱,所以请在这个问题上指导我
I am very weak in jquery, so please guide me on this issue
推荐答案
能否请您尝试以下代码:
Can you please try below code:
jQuery:
$(document).ready( function(){
var meta_image_frame_gallery;
$( '#additional_image_1' ).click(function( event ) {
event.preventDefault();
if ( meta_image_frame_gallery ) {
meta_image_frame_gallery.open();
return;
}
// create new media frame
// You have to create new frame every time to control the Library state as well as selected images
meta_image_frame_gallery = wp.media.frames.wp_media_frame = wp.media( {
title: 'My Gallery', // it has no effect but I really want to change the title
frame: "post",
//toolbar: 'main-gallery',
state: 'gallery-library',
library: {
type: 'image'
},
multiple: true,
} );
/* Add image gallery shortcode in itv_additional_image_1 input filed when close event call */
meta_image_frame_gallery.on('close',function() {
//fetch the gallery state
var controller = meta_image_frame_gallery.states.get('gallery-library');
var library = controller.get('library');
//get the shortcode and update the input field
var new_shortcode = wp.media.gallery.shortcode(library).string();
$('#itv_additional_image_1').val(new_shortcode);
});
/* Update event for image gallery */
meta_image_frame_gallery.on('update', function () {
var controller = meta_image_frame_gallery.states.get('gallery-edit');
var library = controller.get('library');
var new_shortcode = wp.media.gallery.shortcode(library).string(); // Get the new/updated shortcode here.
$('#itv_additional_image_1').val(new_shortcode);
});
});
});
代码经过了完美的测试.更新图库项目也可以完美工作.
这篇关于如何在meta字段中创建画廊时获取wp画廊的简码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文