通过App上传时,JSON API Wordpress无法显示特色图像

时间:2017-01-06 07:47:26

标签: json wordpress

我正面临一些未知问题的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";

对此的任何帮助都将受到高度赞赏。

截图

通过app上传的图片的DB post_type附件 enter image description here

DB Post链接到图像ID enter image description here

缺少图像预览: enter image description here

1 个答案:

答案 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);