模板似乎以脚本形式存在
<script type="text/html" id="tmpl-my-custom-gallery-setting">
要呈现上述模板,需要
wp.media.template(\'my-custom-gallery-setting\')(view)
因为我们要更换
template:
逻辑上,我们所需要做的就是存储一个模板ID列表。
if (!wp.media.gallery.templates) wp.media.gallery.templates = [\'gallery-settings\'];
wp.media.gallery.templates.push(\'my-custom-gallery-setting\');
然后遍历所有可用视图
wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
template: function (view) {
var output = \'\';
for (var i in wp.media.gallery.templates) {
output += wp.media.template(wp.media.gallery.templates[i])(view);
}
return output;
}
});
<小时>
RESULT
因此,现在客户端不必记住短代码属性来修改库,因为所有选项都已作为下拉菜单添加到UI中。
[gallery link="none" type="owl" shape="square" ids="7228,7227,7226,7128,7127"]
另外,支持的类型列表将通过过滤器传递,以便您可以添加或减少来自第三方来源的选择数量。
LOCATION A
add_action(\'print_media_templates\', function() {
// define your backbone template;
// the "tmpl-" prefix is required,
// and your input field should have a data-setting attribute
// matching the shortcode name
$gallery_types = apply_filters(\'print_media_templates_gallery_settings_types\',
array(
\'swiper\' => \' Swiper\',
\'owl\' => \' Owl Carousel\',
\'revolution\' => \' Revolution Slider\',
\'default_val\' => \' Default\'));
?>
<script type="text/html" id="tmpl-custom-gallery-type-setting">
<label class="setting">
<span><?php _e(\'Layout Type\'); ?></span>
<select data-setting="type"><?php
foreach($gallery_types as $key => $value) {
echo "<option value=\\"$key\\">$value</option>";
}
?>
</select>
</label>
</script>
<script>
jQuery(document).ready(function () {
// add your shortcode attribute and its default value to the
// gallery settings list; $.extend should work as well...
_.extend(wp.media.gallery.defaults, {
type: \'default_val\'
});
// join default gallery settings template with yours -- store in list
if (!wp.media.gallery.templates) wp.media.gallery.templates = [\'gallery-settings\'];
wp.media.gallery.templates.push(\'custom-gallery-type-setting\');
// loop through list -- allowing for other templates/settings
wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
template: function (view) {
var output = \'\';
for (var i in wp.media.gallery.templates) {
output += wp.media.template(wp.media.gallery.templates[i])(view);
}
return output;
}
});
});
</script>
<?php
});
一
wp.media.template(\'my-custom-gallery-setting\')(view)