以下代码在db中添加帖子但不上传文件而不创建缩略图,
$filename=$_FILES["image1"]["tmp_name"];
$post_id=1179;
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_name' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_parent' => $post_id,
'post_excerpt' => $thumb_credit,
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment($attachment, $filename, $post_id);
if($attachment_id != 0) {
wp_update_attachment_metadata($attachment_id, $attach_data);
update_post_meta($post_id, '_thumbnail_id', $attachment_id);
}
它的工作是在db中发布,但图片没有上传,
答案 0 :(得分:0)
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
根据文档,您应该参考image.php
文件。