如何将表单中上传的图像设置为帖子的特色图像。
以下是我的基本代码:
用户填写表单并上载图像。提交的表单数据显示为自定义帖子类型。上传的图片将是定制帖子的特色图片。
代码
<form method=\'post\' enctype=\'multipart/form-data\'>
First Name: <input type=\'text\' name=\'fname\'>
Message: <textarea name=\'message\'></textarea>
Photo: <input type=\'file\' name=\'file\'>
<input type=\'submit\' name=\'submit\'>
</form>
<?php if(isset($_POST[\'submit\']) && !empty($_POST)) {
//Upload Image
require_once( ABSPATH . \'wp-admin/includes/image.php\' );
require_once( ABSPATH . \'wp-admin/includes/file.php\' );
require_once( ABSPATH . \'wp-admin/includes/media.php\' );
$attachment_id = media_handle_upload(\'file\',30);
if (is_wp_error($attachment_id)) {
echo \'BIG TIME ERROR\'; }
else {
echo \'BIG TIME SUCCESS\';}
//Insert Post
$my_post = array(
\'post_title\' => $_POST[\'fname\'],
\'post_content\' => $_POST[\'message\'],
\'post_status\' => \'draft\',
\'post_author\' => 1,
\'post_category\' => array(8,39),
\'post_type\' => \'my_custom_post\'
);
wp_insert_post( $my_post );
//Set Image as thumbnail
set_post_thumbnail($my_post, $attachment_id); //Don\'t know what to do with this
?>
我已经做了研究,它的功能是
set_post_thumbnail
或
update_post_meta
?
如果您能提供更多信息,我将不胜感激