我正在为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();任何帮助我们都将不胜感激
发布于 2014-02-14 00:20:18
使用set_post_thumbnail。您可以传入post对象或ID,缩略图ID为媒体附件ID。
set_post_thumbnail( $post, $thumbnail_id );https://stackoverflow.com/questions/21758674
复制相似问题