我正在创建一个插件,它的一部分应该是向自定义帖子添加文件。我设法使脚本正常工作,但经过几次更改后,为了更好的外观和功能,它只允许添加一个文件,而不允许添加其他文件。你能帮帮我吗?
jQuery(document).ready(function($){
var custom_uploader;
$(\'#songbook_addfile_button\').click(function(e) {
e.preventDefault();
//If the uploader object has already been created, reopen the dialog
if (custom_uploader) {
custom_uploader.open();
return;
}
//Extend the wp.media object
custom_uploader = wp.media.frames.file_frame = wp.media({
title:"blemblem",
button: {
text:"blemblem"
},
multiple: true
});
custom_uploader.on(\'select\', function() {
var selection = custom_uploader.state().get(\'selection\');
selection.map( function( attachment ) {
attachment = attachment.toJSON();
extension = extension(attachment.url).replace(\'.\',\'\');
alert(extension);
//...one commented line, that was to add files into HTML structure - works perfect, but only once
});
});
custom_uploader.open();
});
});
从URL获取扩展的fc是:
function extension(url) {
var ext=(url = url.substr(1 + url.lastIndexOf("/")).split(\'?\') [0]).substr(url.lastIndexOf("."));
return ext;
};
单击“打开媒体管理器”按钮时,它会打开
选择一个并单击添加介质时,其已添加(或在此代码中发出警报)
选择更多文件时,只需要第一个
添加第一个文件后,单击即可打开,但单击“管理器添加”按钮时不要添加任何文件。
拜托,你能告诉我怎么走吗?非常感谢。
最合适的回答,由SO网友:cybmeta 整理而成
我对您的代码进行了一些编辑alert()
为每个选定项目运行。请注意,在代码中执行return
太早了,这会使下面的代码在第一次运行后不运行。此外,函数和字符串变量使用相同的名称,这并不太合适。
function get_the_extension(url) {
var ext=(url = url.substr(1 + url.lastIndexOf("/")).split(\'?\')[0]).substr(url.lastIndexOf("."));
return ext;
}
jQuery(document).ready(function($){
var custom_uploader;
$(\'#songbook_addfile_button\').click(function(e) {
e.preventDefault();
//If the uploader object has already been created, reopen the dialog
if (custom_uploader) {
custom_uploader.open();
}
//Extend the wp.media object
custom_uploader = wp.media.frames.file_frame = wp.media({
title:"blemblem",
button: {
text:"blemblem"
},
multiple: true
});
custom_uploader.on(\'select\', function() {
var selection = custom_uploader.state().get(\'selection\');
selection.map( function( attachment ) {
attachment = attachment.toJSON();
var the_extension = get_the_extension(attachment.url).replace(\'.\',\'\');
alert(the_extension);
//...one commented line, that was to add files into HTML structure - works perfect, but only once
});
});
custom_uploader.open();
});
});