在以编程方式插入的帖子中添加帖子缩略图

时间:2013-04-05 作者:10wtaylor

我想以编程方式插入post,下面是添加post的代码:

global $user_ID;
$new_post = array(
    \'post_title\' => \'My New Post\',
    \'post_content\' => \'Lorem ipsum dolor sit amet...\',
    \'post_status\' => \'publish\',
    \'post_date\' => date(\'Y-m-d H:i:s\'),
    \'post_author\' => $user_ID,
    \'post_type\' => \'post\',
    \'post_category\' => array(0)
);
$post_id = wp_insert_post($new_post);
如何在帖子中添加特色图片并触发它进行测试?

3 个回复
最合适的回答,由SO网友:helgatheviking 整理而成

使用以下键将帖子缩略图保存为帖子元:_thumbnail_id. 因此,在插入帖子并获取帖子id后,可以为该帖子设置帖子元。这个$thumbnail_id 只是您想设置为缩略图的图像的ID,由您决定,因为我无法从您的问题中判断这应该是什么。

global $user_ID;
$new_post = array(
    \'post_title\' => \'My New Post\',
    \'post_content\' => \'Lorem ipsum dolor sit amet...\',
    \'post_status\' => \'publish\',
    \'post_date\' => date(\'Y-m-d H:i:s\'),
    \'post_author\' => $user_ID,
    \'post_type\' => \'post\',
    \'post_category\' => array(0)
);
$post_id = wp_insert_post($new_post);

if( ! is_wp_error( $post_id ) )
       update_post_meta( $post_id, \'_thumbnail_id\', $thumbnail_id );

SO网友:Aurovrata

使用该功能set_post_thumbnail,

//$file is the path to your uploaded file (for example as set in the $_FILE posted file array)
//$filename is the name of the file
//first we need to upload the file into the wp upload folder. 
$upload_file = wp_upload_bits($filename, null, @file_get_contents($file));
if(!$upload_file[\'error\']) {
  //if succesfull insert the new file into the media library (create a new attachment post type)
  $wp_filetype = wp_check_filetype($filename, null );
  $attachment = array(
    \'post_mime_type\' => $wp_filetype[\'type\'],
    \'post_parent\' => $post_id,
    \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', $filename),
    \'post_content\' => \'\',
    \'post_status\' => \'inherit\'
  );
  //wp_insert_attachment( $attachment, $filename, $parent_post_id );
  $attachment_id = wp_insert_attachment( $attachment, $upload_file[\'file\'], $post_id );
  if (!is_wp_error($attachment_id)) {
     //if attachment post was successfully created, insert it as a thumbnail to the post $post_id
     require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
     //wp_generate_attachment_metadata( $attachment_id, $file ); for images
     $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file[\'file\'] );
     wp_update_attachment_metadata( $attachment_id,  $attachment_data );
     set_post_thumbnail( $post_id, $attachment_id );
   }
}

SO网友:dipali

 $wp_filetype = wp_check_filetype(basename($filename), null );
  $wp_upload_dir = wp_upload_dir();
  $attachment = array(
     \'guid\' => $wp_upload_dir[\'url\'] . \'/\' . basename( $filename ), 
     \'post_mime_type\' => $wp_filetype[\'type\'],
     \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($filename)),
     \'post_content\' => \'\',
     \'post_status\' => \'inherit\'
  );
  $attach_id = wp_insert_attachment( $attachment, $filename, $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 );
有关更多信息,请参阅此链接https://developer.wordpress.org/reference/functions/wp_insert_attachment/

结束

相关推荐