我的网站上有一个表单。
这是用户注册的一个失踪人员。我创建了一个自定义post类型missing person、自定义分类法和自定义字段。所有这些输入也保存在wp中。
但我不知道提交一张图片并将此图片保存为特色图片。
$my_post = array(
\'post_title\' => $_POST[\'title\'],
\'post_date\' => $_SESSION[\'cal_startdate\'],
\'post_content\' => $_POST[\'myContent\'],
\'post_status\' => \'draft\',
\'post_type\' => \'pessoa_desaparecida\',
);
$post_id = wp_insert_post($my_post);
$uploaddir = wp_upload_dir();
$file = $_FILES["test"]["name"];
$uploadfile = $uploaddir[\'path\'] . \'/\' . basename( $file );
move_uploaded_file( $file , $uploadfile );
$filename = basename( $uploadfile );
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', $filename),
\'post_content\' => \'\',
\'post_status\' => \'inherit\',
\'menu_order\' => $_i + 1000
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
update_post_meta($post_id,\'_thumbnail_id\',$attach_id);
set_post_thumbnail( $post_id, $thumbnail_id );
但不起作用
我在前台的表单是:
<form action="/homolog/cadastro" method="post" enctype="multipart/form-data">
<input type="text" name="title" />
<input type="file" size="20" name="test" />
</form>
打印:
SO网友:Bindiya Patoliya
您可以这样尝试:
set_post_thumbnail( $my_post_id, $thumbnail_id );
您必须先按以下方式将图像添加到库中:
$uploaddir = wp_upload_dir();
$file = $_FILES[ ... whatever you have in your POST data ... ];
$uploadfile = $uploaddir[\'path\'] . \'/\' . basename( $file );
move_uploaded_file( $file , $uploadfile );
$filename = basename( $uploadfile );
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
\'post_mime_type\' => $wp_filetype[\'type\'],
\'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', $filename),
\'post_content\' => \'\',
\'post_status\' => \'inherit\',
\'menu_order\' => $_i + 1000
);
$attach_id = wp_insert_attachment( $attachment, $uploadfile );
有关更多详细信息,请参阅
this link