我正面临一些未知问题的wordpress特色图片。当我从网站更新精选图片时;它通过json显示在应用程序中。
http://indiafastener.com/api/?json=get_post&post_type=listing-item&id=1377
但是当我通过json将图像上传到wordpress db时;图像字段中的输出为NULL。
http://indiafastener.com/api/?json=get_post&post_type=listing-item&id=1380
当我看到db;它有图像路径,路径不会导致404.
图片路径 http://www.indiafastener.com/webservices/listing/uploads/2017-04-01_12-01-40IMG-20150715-WA0004.jpg
可能是因为wp-content/uploads/2016/02/
文件夹中没有图片吗?
上传图片的代码
require_once('../../wp-config.php');
require_once('../../wp-admin/includes/image.php');
$dirname = "../../wp-content/uploads/2017/01/";
$filename = $_FILES["file"]["name"];
$attachment = array(
'post_mime_type' => 'image/jpeg',
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $dirname.basename($filename)
//'wp-content/uploads/2017/01/' . basename($filename)
);
$your_post_id = 1392;
$attach_id = wp_insert_attachment( $attachment, $filename,'$your_post_id' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
//$id=1385;
update_post_meta($id, '_thumbnail_id', $attach_id);
echo "success";
对此的任何帮助都将受到高度赞赏。
截图
答案 0 :(得分:4)
我不确定您的代码,但我的下面的代码完美地用于将图像指定为帖子的特征图像。请仔细阅读。 附件ID应分配给微粒帖子,检查你的帖子meta' _thumbnail_id'也适用于帖子。
require_once(ABSPATH . 'wp-admin/includes/image.php');
$filename = 'your file name';
$attachment = array(
'post_mime_type' => 'your mime type',
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $wp_upload_dir['url'] . '/' . basename($filename)
);
$attach_id = wp_insert_attachment( $attachment, $filename,'your_post_id' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_post_meta($id, '_thumbnail_id', $attach_id);