单击上载文件时,选择该文件,然后单击选择文件。我希望能够在单击更新之前或之后再次单击上载文件,并将所选文件添加到“my\\u file\\u URL”和“my\\u file\\u ID”元框中的现有值。但我在如何着手做这件事上遇到了一些困难。现在,它将删除以前的值。非常感谢您的帮助。非常感谢。代码如下:
功能。php
function add_resource($post){
$url = get_post_meta($post->ID, \'my_file_URL\', true);
$id = get_post_meta($post->ID, \'my_file_ID\', true);
?>
<input id="my_file_URL" name="my_file_URL" type="text"
value="<?php echo $url; ?>" style="width:400px;" />
<input id="my_file_ID" name="my_file_ID" type="hidden"
value="<?php echo $id; ?>" style="width:400px;" />
<input id="my_upl_button" type="button" value="Upload File" /><br/>
<script>
jQuery(document).ready(function($){
var custom_uploader;
$(\'#my_upl_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: \'Choose File\',
button: {
text: \'Choose File\'
},
multiple: true
});
//When a file is selected, grab the URL and set it as the text field\'s value
custom_uploader.on(\'select\', function() {
var selection = custom_uploader.state().get(\'selection\').toJSON();
var urls = selection.map( function(attachment){
return attachment.url;
});
var ids = selection.map( function(attachment){
return attachment.id;
});
for(i=0; i < selection.length; i++){
$( \'#my_file_URL\' ).after(
\'<br/><p>File URL: \' + selection[i].url + \'</p><p> Description: \' + selection[i].description + \'</p><br/>\'
);
}
$( \'#my_file_URL\' ).val(urls.join(\',\'));
$( \'#my_file_ID\' ).val(ids.join(\',\'));
});
//Open the uploader dialog
custom_uploader.open();
});
});
<?php
}
function save_meta_box($post_id){
global $post;
if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE){
return;
} else {
if (isset($_POST[\'my_file_URL\'])){
if(is_array($url))
$url[] = $new_url;
else
$url = array($new_url);
update_post_meta($post_id, \'my_file_URL\', $_POST[\'my_file_URL\']);
}
if (isset($_POST[\'my_file_ID\'])){
if(is_array($id))
$id[] = $new_id;
else
$id = array($new_id);
update_post_meta($post_id, \'my_file_ID\', $_POST[\'my_file_ID\']);
}
}
}
add_action(\'save_post\',\'save_meta_box\');
单身。php
$url = get_post_meta($post->ID, \'my_file_URL\', true);
$id = get_post_meta($post->ID, \'my_file_ID\', true);
$urls = explode(\',\', $url);
$urlCount = count($urls);
$ids = explode(\',\', $id);
for($i=0; $i < $urlCount; $i++){
$fileID = $ids[$i];
$attachment_filesize = size_format( filesize( get_attached_file( $fileID ) ), 2 );
$date = get_the_date(\'M d, Y\', $fileID);
$attachment = get_post( $fileID ); ?>
<div class="codesc" style="float:left;"><h3><a href="<?php echo $urls[$i] ?>" target="_blank"><?php echo $attachment->post_title; ?><br>
<span class="linkdetails"><?php echo $attachment_filesize; ?> | <?php echo $date; ?></span></a></h3>
<p><?php echo $attachment->post_content; ?></p>
</div>
<?php
} ?>