这是我的php,用于创建带有缩略图的帖子。我想知道如何将上传的图像添加到帖子内容中。尝试了一些事情,但都没有成功。
if(isset($_POST[\'new_post\']) == \'1\') {
$post_title = $_POST[\'post_title\'];
$post_category = $_POST[\'cat\'];
$filename = $_POST[\'attach\'];
$new_post = array(
\'ID\' => \'\',
\'post_author\' => $current_user->ID,
\'post_category\' => array($post_category),
\'post_title\' => $post_title,
\'post_status\' => \'draft\'
);
$post_id = wp_insert_post($new_post);
$post = get_post($post_id);
$new_post = $post->ID;
if (!function_exists(\'wp_generate_attachment_metadata\')){
require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');
}
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file][\'error\'] !== UPLOAD_ERR_OK) {
return "upload error : " . $_FILES[$file][\'error\'];
}
$attach_id = media_handle_upload( $file, $new_post );
}
}
if ($attach_id > 0){
//and if you want to set that image as Post then use:
update_post_meta($new_post,\'_thumbnail_id\',$attach_id);
}
}