我尝试使用wordpress的媒体管理器我使用admin wordpress外的帖子编辑器,用户可以创建一个特色图片的帖子。 我使用_wp_post_thumbnail_html函数来显示图片或显示上传文件的链接,所有用户都可以上传图片并上传作品,但无法显示精选图片或分配到帖子。
on wp-ajax whit action:set-post-thumbnail返回0,图片未分配给新帖子。 WP-ajax.php:
json:true thumbnail_id:3952 _wpnonce:b02e8553f1动作:set-post-thumbnail
回复:0
我的代码为:
<?php $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
//$postid = get_post( $post_id );
echo _wp_post_thumbnail_html( $thumbnail_id, $post_id );
?>
<br>
非常简单,从wordpres显示媒体管理器,允许上传精选图片但不允许分配到新帖子。任何解决方案?
编辑:在编辑帖子工作正常允许更改特色图片我想因为特色图片需要一个帖子ID,但在wordpress的新帖子上允许上传图片并将此作为新帖子。
答案 0 :(得分:1)
这是正确的图片上传代码。
<?php
global $wpdb;
include ('../../../../wp-load.php');
if ( $_FILES ) {
if(!function_exists('wp_handle_upload')){
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
$upload_overrides = array('test_form' => 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代码,请转到此link。如果您有任何问题,请在下面发表评论。