上传的附件未设置为特色图像

时间:2013-10-17 作者:codecowboy

下面的代码上载了一个我想与自定义帖子类型关联的图像。

$upload_overrides = array( \'test_form\' => FALSE );
if($file_errors[\'error\'] == 0) {

  if($new_reseller_id = wp_insert_post( $new_reseller_profile_data )){

    //$this->wps_process_image($file, $new_reseller_id);exit;

    $uploaded_file = wp_handle_upload( $file, $upload_overrides );

    $wp_filetype = wp_check_filetype(basename($uploaded_file[\'file\']), null );
    $wp_upload_dir = wp_upload_dir();
    $attachment = array(
      \'guid\' => $wp_upload_dir[\'url\'] . \'/\' . basename( $uploaded_file[\'file\'] ),
      \'post_mime_type\' => $wp_filetype[\'type\'],
      \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($file[\'name\'])),
      \'post_content\' => \'\',
      \'post_status\' => \'inherit\'
    );
    $attach_id = wp_insert_attachment( $attachment, $uploaded_file[\'file\'], $new_reseller_id );
    // you must first include the image.php file
    // for the function wp_generate_attachment_metadata() to work
    $attach_data = wp_generate_attachment_metadata( $attach_id, $uploaded_file[\'file\'] );
    wp_update_attachment_metadata( $attach_id, $attach_data );

  }
}
图像已上载并可在图像库中查看,但不会在自定义帖子类型编辑器中显示为特征图像。为什么会这样?

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

我需要做以下工作:

set_post_thumbnail( $new_reseller_id, $attach_id );

SO网友:Ari

您还可以使用update_post_meta

// Set Featured Image
update_post_meta($new_reseller_id, \'_thumbnail_id\', $attach_id);

结束

相关推荐

Resize uploaded images

Possible Duplicate:Resizing all images 我有一个我建立的新闻门户,客户想要不同大小的特色图片。我已经准备好了我想要的第一个尺寸,他们已经发布了大约200多篇帖子,都准备好了这个尺寸。现在,如果我改变大小,它只会在新帖子上改变/或重新上传当前的特色图片(手工操作太多了)。我的问题是,有没有办法调整上传图像的大小?