set-post-thumbnail wp-ajax返回0

我尝试使用wordpress的媒体管理器,我使用admin wordpress外的post编辑器,用户可以创建一个特色图像的post。 我使用_wp_post_thumbnail_html函数来显示图像或显示上传文件的链接,所有用户都可以上传图片和上传作品,但无法显示特色图片或分配到post。

在wp-ajax whit动作:set-post-thumbnail返回0,图像不分配给新post。 WP-ajax.php:

json:true thumbnail_id:3952 _wpnonce:b02e8553f1 action:set-post-thumbnail

回复:0

我的代码为:

 

非常简单,从wordpres显示媒体管理器,允许上传精选图片但不允许分配给新post。 有解决方案吗

编辑:在编辑post工作正常允许更改特色图片我想因为特色图片需要一个postID,但在wordpress的新post上允许上传图片并将此作为新post。

这是正确的图像上传代码。

  false); $response = array(); ini_set('max_execution_time', 0); foreach($_FILES as $file){ $movefile_profile = wp_handle_upload($file, $upload_overrides); $filename = $movefile_profile['url']; $parent_post_id = $post_id; // please provide post id must $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' => $filetype['type'], 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id ); $filepath = $wp_upload_dir['path'] . '/' . basename( $filename ); $attach_data = wp_generate_attachment_metadata( $attach_id, $filepath ); wp_update_attachment_metadata( $attach_id, $attach_data ); set_post_thumbnail($post_id, $attach_id); // this set_post_thumbnail will set you post thumbnail $response['message'] = 'Done'; } echo json_encode($response); die(); } 

对于Ajax代码,请转到此链接 。 如果您有任何问题,请在下面发表评论。