I am developing a plugin that would allow a user to write new posts directly from the front end. Everything is fine, but I can’t find any information on how to attach a uploaded file to the newly created post so that I could call the image this way: the_post_thumbnail( ‘medium’ ). (an image with the thumbnail is returned).
The strange thing is that calling the_post_thumbnail( ‘thumbnail’ ) works just fine…
Here is the code I wrote for the thumbail:
$attachment_thumbnail_filename = image_resize($upload_preview["file"], get_option("thumbnail_size_w"), get_option("thumbnail_size_h"), true, get_option("thumbnail_size_w")."_".get_option("thumbnail_size_h"));</p>
<p>$wp_filetype = wp_check_filetype(basename($attachment_thumbnail_filename), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($attachment_thumbnail_filename)),
'post_content' => '',
'post_type' => 'attachment',
'post_status' => 'inherit'
<p>$attach_id = wp_insert_attachment($attachment, $attachment_thumbnail_filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $attachment_thumbnail_filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_post_meta( $post_id, '_thumbnail_id', $attach_id );
Since this works fine to call the thumbnail, I thought that adapting the code for the medium size would work just fine! but not!
Do you have any idee on where I could finde some ressources about this topic?
thanks a lot for your help!
- The topic ‘upload and the_post_thumbnail( ‘medium’ )’ is closed to new replies.