我正在为wordpress创建一个自定义的帖子提交表单,目前我可以创建帖子并将图片上传并附加到帖子但下一步我正在努力 - 我希望上传的图片自动显示在帖子上,香港专业教育学院调查了特色图片选项,但却无法让它工作
$my_post = array();
$my_post['post_title'] = $newid;
$my_post['post_content'] = $imageurl1.$imageurl.$config_basedir.$uploaddir.$id . ".gif". $imageurl2;
$my_post['post_author'] = 1;
$my_post['post_status'] = draft;
$my_post['post_category'] = array($a);
// Insert the post into the database
$post_id = wp_insert_post($my_post);
//
$wp_filetype = wp_check_filetype(basename($id . ".gif"), null );
$attachment = array();
$attachment['post_mime_type'] = $wp_filetype['type'];
$attachment['post_title'] = $newid;
$attachment['post_content'] = '';
$attachment['post_status'] = 'inherit';
$attach_id = wp_insert_attachment($attachment, $id . ".gif", $post_id)
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
set_post_thumbnail($post_id, $attach_id);
//
wp_redirect( site_url()."?p=$post_id" ); exit();
感谢任何帮助
答案 0 :(得分:0)
使用set_post_thumbnail。您可以传入post对象或ID。缩略图ID是媒体附件ID。
set_post_thumbnail( $post, $thumbnail_id );