我已经为此做了一个简单的函数,但我允许所有类型的媒体。所以这可能是一个开始:
function insert_attachment( $file_handler, $post_id, $settpdf=\'false\' ) {
// check to make sure its a successful upload
if ( $_FILES[$file_handler][\'error\'] !== UPLOAD_ERR_OK ) __return_false();
require_once( ABSPATH . \'wp-admin\' . \'/includes/image.php\' );
require_once( ABSPATH . \'wp-admin\' . \'/includes/file.php\' );
require_once( ABSPATH . \'wp-admin\' . \'/includes/media.php\' );
$attach_id = media_handle_upload( $file_handler, $post_id );
if ( $settpdf ) update_post_meta( $post_id,\'_pdf_id\', $attach_id );
return $attach_id;
}
然后输入如下:
<input type="file" name="uploaded_pdf">
您可以如下方式检索元:
$pdf = get_post_meta($post->ID, \'_pdf_id\', true);
echo \'<a href="\'.$pdf.\'"><?php __(\'Download PDF\',\'mytheme\'); ?></a>\';