말 그대로 예제다. 시간이 없어서 설명은 생략하고 코드만.
if( ! empty($_FILES)){
$wp_upload_dir = wp_upload_dir();
require_once(ABSPATH . 'wp-admin/includes/file.php');
$fileinfo = wp_handle_upload($_FILES['첨부파일'], array('test_form' => FALSE ));
$wp_filetype = wp_check_filetype(basename($fileinfo['file']), null );
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($fileinfo['file']),
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($fileinfo['file'])),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $fileinfo['file'], $post_id);
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $fileinfo['file'] );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
클라이언트 쪽은 다음 코드를 참고. 역시 시간이 없어서 foreach
를 못 돌렸다. 알아서 돌리길.
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if($attachments){?>
<p>
첨부파일 :
<a href="<?=wp_get_attachment_url($attachments[0]->ID)?>">
<?=apply_filters('the_title', $attachments[0]->post_title);?>
</a>
</p>
<?}?>
댓글 남기기