我最终做的是extending the WP_Customize_Control
分类如下:
<?php
if (!class_exists(\'WP_Customize_Image_Control\')) {
return null;
}
class Multi_Image_Custom_Control extends WP_Customize_Control
{
public function enqueue()
{
wp_enqueue_style(\'multi-image-style\', get_template_directory_uri().\'/css/multi-image.css\');
wp_enqueue_script(\'multi-image-script\', get_template_directory_uri().\'/js/multi-image.js\', array( \'jquery\' ), rand(), true);
}
public function render_content()
{ ?>
<label>
<span class=\'customize-control-title\'>Image</span>
</label>
<div>
<ul class=\'images\'></ul>
</div>
<div class=\'actions\'>
<a class="button-secondary upload">Add</a>
</div>
<input class="wp-editor-area" id="images-input" type="hidden" <?php $this->link(); ?>>
<?php
}
}
?>
当用户单击“添加”时,我使用javascript打开WP媒体选择器。选择图像时,图像必须显示在内部
<ul class=\'images\'></ul>
. 此外,用户需要能够通过单击图像来删除图像。我做了如下
javascript file:
( function( $ ) {
$(window).load(function(){
var begin_attachment_string = $("#images-input").val();
var begin_attachment_array = begin_attachment_string.split(",");
for(var i = 0; i < begin_attachment_array.length; i++){
if(begin_attachment_array[i] != ""){
$(".images").append( "<li class=\'image-list\'><img src=\'"+begin_attachment_array[i]+"\'></li>" );
}
}
$(".button-secondary.upload").click(function(){
var custom_uploader = wp.media.frames.file_frame = wp.media({
multiple: true
});
custom_uploader.on(\'select\', function() {
var selection = custom_uploader.state().get(\'selection\');
var attachments = [];
selection.map( function( attachment ) {
attachment = attachment.toJSON();
$(".images").append( "<li class=\'image-list\'><img src=\'"+attachment.url+"\'></li>" );
attachments.push(attachment.url);
//
});
var attachment_string = attachments.join() + "," + $(\'#images-input\').val();
$(\'#images-input\').val(attachment_string).trigger(\'change\');
});
custom_uploader.open();
});
$(".images").click(function(){
var img_src = $(event.target).find("img").attr(\'src\');
$(event.target).closest("li").remove();
var attachment_string = $(\'#images-input\').val();
attachment_string = attachment_string.replace(img_src+",", "");
$(\'#images-input\').val(attachment_string).trigger(\'change\');
});
});
} )( jQuery );
最后,我补充道
some CSS:
.image-list{
width: 100%;
height: 150px;
background-position: center;
background-size: cover;
background-repeat: no-repeat;
-webkit-box-shadow: inset 0 0 15px rgba(0,0,0,.1), inset 0 0 0 1px rgba(0,0,0,.05);
box-shadow: inset 0 0 15px rgba(0,0,0,.1), inset 0 0 0 1px rgba(0,0,0,.05);
background: #eee;
cursor: pointer;
vertical-align: middle;
display:flex;
justify-content:center;
align-items:center;
overflow: hidden;
position: relative;
}
.image-list:before{
content: \'\';
position: absolute;
display: none;
top: 0px;
right: 0px;
left: 0px;
bottom: 0px;
box-shadow: inset 0 0 0 1px #fff, inset 0 0 0 5px #c60c31;
}
.image-list:hover:before{
display: block;
}