質問: theme_prefix . "slider_text"; ?> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/ajaxupload.3.5.js" ></script> <script type="text/javascript" > $(function(){ var btnupload=$('#slider-upload'); var status=$('#slider-upload-status'); new ajaxupload(btnupload, { action: '<?php bloginfo('template_directory'); ?>/upload-file.php', name: 'uploadfile', onsubmit: function(file, ext){ if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ // extension is not allowed status.text('only jpg, png or gif files are allowed'); return false; } status.text('uploading...'); }, oncomplete: function(file, response){ //on completion clear the status status.text(''); //add uploaded file to list if(response==="success"){ $('<li></li>').appendto('#files').html('<img src="<?php bloginfo('template_directory'); ?>/images/'+file+'" alt="" /><br />'+file).addclass('success'); $('#<?php echo $slider_image; ?>').val(file); } else{ $('<li></li>').appendto('#files').text(file).addclass('error'); } } }); }); </script> <div class="postbox"> <h3>slider customization options</h3> <div class="postbox-content"> <div class="option-row"> <p>choose how you would like to display your slider.</p> </div><!--end option-row--> <div class="option-row"> <div class="option-name"> <label>use image for slider</label> </div><!--end option-name--> <div class="option-value"> <input class="checkbox" id="<?php echo $slider_image_enabled; ?>" type="checkbox" name="<?php echo $slider_image_enabled; ?>" value="true"<?php checked(true, (bool) get_option($slider_image_enabled)); ?> /> </div><!--end option-value--> </div><!--end option-row--> <div class="option-row"> <div class="option-name"> <label>upload an image</label> <span id="slider-upload-status"></span> </div><!--end option-name--> <div class="option-value"> <input class="logo-name" id="<?php echo $slider_image; ?>" type="text" name="<?php echo $slider_image; ?>" value="<?php echo get_option($slider_image); ?>" /> <input type="button" class="background_pattern_button" id="slider-upload" value="choose image" /> </div><!--end option-value--> </div><!--end option-row--> <div class="option-row"> <div class="option-name"> <label>slider text</label> </div><!--end option-name--> <div class="option-value"> <input class="input-box" id="<?php echo $slider_text; ?>" type="text" name="<?php echo $slider_text; ?>" value="<?php echo get_option($slider_text) ?>" /> </div><!--end option-value--> </div><!--end option-row--> <input type="submit" class="button" value="save changes" /> </div> </div><!--end postbox--> front-page.php: <div id="slider"> <img src="<?php bloginfo('template_directory'); ?>/images/<?php echo get_option(theme_prefix . 'slider_image'); ?>" /> </div>
回答: $_files配列をループ処理して、各ファイルを別々に追加します。私はテーマのオプションクラスから抜粋した、完全なコード例は手元にはありません。/** * saves uploaded files in media library and the corresponding id in option field. * * @return void */ protected function handle_uploads() { if ( ! isset ( $_files ) or empty ( $_files ) ) { return; } foreach ( $_files as $file_key => $file_arr ) { // some bogus upload. if ( ! isset ( $this->fields[$file_key] ) or empty ( $file_arr['type'] ) ) { continue; } if ( ! $this->is_allowed_mime( $file_key, $file_arr ) ) { set_theme_mod( $file_key . '_error', 'wrong mime type' ); continue; } // the file is allowed, no error until now and the type is correct. $uploaded_file = wp_handle_upload( $file_arr , array( 'test_form' => false ) ); // error if ( isset ( $uploaded_file['error'] ) ) { set_theme_mod( $file_key . '_error', $uploaded_file['error'] ); continue; } // add the file to the media library // set up options array to add this file as an attachment $attachment = array( 'post_mime_type' => $uploaded_file['type'] , 'post_title' => $this->get_media_name( $file_key, $uploaded_file['file'] ) ); // adds the file to the media library and generates the thumbnails. $attach_id = wp_insert_attachment( $attachment , $uploaded_file['file'] ); $this->create_upload_meta( $attach_id, $uploaded_file['file'] ); // update the theme mod. set_theme_mod( $file_key, $attach_id ); remove_theme_mod( $file_key . '_error' ); } } handle_uploads()は、同じクラスからsave_options()によって呼び出されます。 is_allowed_mime()は必要な型だけをファイルすることを保証します、そしてget_media_name()はそのファイルの特別な事前定義された名前を探します("logo"、"bg-body"など)。ご覧のとおり、それほど難しいことではありません。 upload-file.phpのコードではなく、組み込みapiを使用するだけです。 wordpressがあなたの普通のmove_uploaded_file()よりもうまく処理するであろうedgeケースがたくさんあります。