您好,我有一个页面,上面有一个小上载程序,可以在库中上载媒体,下面是我的代码:
<?php
$post_id = $post->ID;
if ( isset( $_POST[\'html-upload\'] ) && !empty( $_FILES ) ) {
require_once(ABSPATH . \'wp-admin/includes/admin.php\');
$id = media_handle_upload(\'async-upload\', $post_id); //post id of Client Files page
unset($_FILES);
if ( is_wp_error($id) ) {
$errors[\'upload_error\'] = $id;
$id = false;
}
if ($errors) {
echo "<p>There was an error uploading your file.</p>";
} else {
echo "<p>Your file has been uploaded.</p>";
}
}
?>
使用我的表格:
<form id="file-form" enctype="multipart/form-data" action="<?php echo $_SERVER[\'REQUEST_URI\']; ?>" method="POST">
<p id="async-upload-wrap"><label for="async-upload">upload</label>
<input type="file" id="async-upload" name="async-upload"> <input type="submit" value="Upload" name="html-upload"></p>
<p><input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id ?>" />
<?php wp_nonce_field(\'client-file-upload\'); ?>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER[\'REQUEST_URI\']; ?>" /></p>
<p><input type="submit" value="Save all changes" name="save" style="display: none;"></p>
<form>
和我的代码段,以启用媒体分类和标记:
// add categories for attachments
function add_categories_for_attachments() {
register_taxonomy_for_object_type( \'category\', \'attachment\' );
}
add_action( \'init\' , \'add_categories_for_attachments\' );
// add tags for attachments
function add_tags_for_attachments() {
register_taxonomy_for_object_type( \'post_tag\', \'attachment\' );
}
add_action( \'init\' , \'add_tags_for_attachments\' );
一切都很好。我可以上传,它正在工作。我可以在背面的媒体库中选择类别。现在我只需要一个复选框或列表来选择existant category,以便在上载之前将其附加到上载的媒体。
我现在不知道我是否清楚,但我不是英语母语。:)
谢谢大家!