我正在尝试自定义媒体浏览器的模板(当单击“编辑帖子”页面中的“添加媒体”按钮时),方法是在每个缩略图上添加带有文件名的标题,这样我就不需要选择图像来检查附件详细信息面板上的文件名。
到目前为止,我所做的是以下所述解决方案的一个版本:https://stackoverflow.com/a/25948448/8717608
我使用的是“tmpl附件”模板,来自媒体模板的第506行。php,这正是我需要定制的:https://github.com/WordPress/WordPress/blob/master/wp-includes/media-template.php
通过单击添加媒体按钮,我原本希望在图像底部的黑色背景中看到所有的拇指及其文件名,但什么都没有发生。看来我遗漏了什么,如果能帮我找出问题所在,我将不胜感激。
add_action( \'wp_enqueue_media\', \'add_media_overrides\' );
function add_media_overrides() {
add_action( \'admin_footer-post.php\', \'override_media_templates\' );
}
function override_media_templates(){
?>
<script type="text/html" id="tmpl-attachment_custom">
<div class="attachment-preview js--select-attachment type-{{ data.type }} subtype-{{ data.subtype }} {{ data.orientation }}">
<div class="thumbnail">
<# if ( data.uploading ) { #>
<div class="media-progress-bar"><div style="width: {{ data.percent }}%"></div></div>
<# } else if ( \'image\' === data.type && data.sizes ) { #>
<div class="centered">
<img src="{{ data.size.url }}" draggable="false" alt="" style="height: 90%; transform: translate(-50%,-55%);"/>
<div style="
transform: translate(-50%,-100%);
background-color: black;
color: white;
margin-top: 50%;
font-size: 80%;
z-index: 9999;
position: relative;
vertical-align: middle;
">{{ data.filename }}</div>
</div>
<# } else { #>
<div class="centered">
<# if ( data.image && data.image.src && data.image.src !== data.icon ) { #>
<img src="{{ data.image.src }}" class="thumbnail" draggable="false" alt="" />
<# } else if ( data.sizes && data.sizes.medium ) { #>
<img src="{{ data.sizes.medium.url }}" class="thumbnail" draggable="false" alt="" />
<# } else { #>
<img src="{{ data.icon }}" class="icon" draggable="false" alt="" />
<# } #>
</div>
<div class="filename">
<div>{{ data.filename }}</div>
</div>
<# } #>
</div>
<# if ( data.buttons.close ) { #>
<button type="button" class="button-link attachment-close media-modal-icon"><span class="screen-reader-text"><?php _e( \'Remove\' ); ?></span></button>
<# } #>
</div>
<# if ( data.buttons.check ) { #>
<button type="button" class="check" tabindex="-1"><span class="media-modal-icon"></span><span class="screen-reader-text"><?php _e( \'Deselect\' ); ?></span></button>
<# } #>
<#
var maybeReadOnly = data.can.save || data.allowLocalEdits ? \'\' : \'readonly\';
if ( data.describe ) {
if ( \'image\' === data.type ) { #>
<input type="text" value="{{ data.caption }}" class="describe" data-setting="caption"
placeholder="<?php esc_attr_e( \'Caption this image…\' ); ?>" {{ maybeReadOnly }} />
<# } else { #>
<input type="text" value="{{ data.title }}" class="describe" data-setting="title"
<# if ( \'video\' === data.type ) { #>
placeholder="<?php esc_attr_e( \'Describe this video…\' ); ?>"
<# } else if ( \'audio\' === data.type ) { #>
placeholder="<?php esc_attr_e( \'Describe this audio file…\' ); ?>"
<# } else { #>
placeholder="<?php esc_attr_e( \'Describe this media file…\' ); ?>"
<# } #> {{ maybeReadOnly }} />
<# }
} #>
</script>
<script>
jQuery(document).ready( function($) {
if( typeof wp.media.view.Attachment != \'undefined\' ){
wp.media.view.Attachment.prototype.template = wp.media.template( \'attachment-custom\' );
}
});
</script>
<?php
}
最合适的回答,由SO网友:Daniel Sian 整理而成
有一个输入错误,而不是:
wp.media.view.Attachment.prototype.template = wp.media.template( \'attachment-custom\' );
应为:
wp.media.view.Attachment.prototype.template = wp.media.template( \'attachment_custom\' );