通过App上传时,JSON API Wordpress不显示特色图片 [英] JSON API Wordpress not showing featured image when uploaded via App

查看:44
本文介绍了通过App上传时,JSON API Wordpress不显示特色图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用wordpress特色图片时遇到了一些未知的问题.当我从网站更新精选图片时;它通过json显示在应用程序中.

I am facing some unknown issues with wordpress featured image. When I update a featured image from the website; it gets displayed in the app via json.

http://indiafastener.com/api/? json = get_post& post_type = listing-item& id = 1377

但是,当我通过json将图像上传到wordpress数据库时; image字段中的输出为NULL.

however when I upload an image via json into the wordpress db; the output is NULL in the image field.

http://indiafastener.com/api/? json = get_post& post_type = listing-item& id = 1380

当我看到数据库时;它具有图像路径,并且该路径不通向404.

when I see the db; it has the image path and the path does not lead to 404.

图像路径: http://www.indiafastener .com/webservices/listing/uploads/2017-04-01_12-01-40IMG-20150715-WA0004.jpg

是因为图像不在wp-content/uploads/2016/02/文件夹中吗?

Could it be because the image is not there in the wp-content/uploads/2016/02/ folder?

上传图片的代码

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";

对此将提供任何帮助,我们将不胜感激.

Any help on this will be highly appreciated.

屏幕截图

DB post_type附件

DB post_type attachment of image uploaded via app

DB Post链接到图像ID

DB Post linked to the image Id

缺少图像预览:

推荐答案

我不确定您的代码,但是我下面的代码非常适合将图像分配为发布的特征图像.请通过它. 附件ID应该分配给特定的帖子,还要检查您的帖子meta'_thumbnail_id'中的帖子.

I am not sure about your code but my below piece of code is working perfectly for assign image as feature image for post. please go through it. attachment id should be assign to particulate post, check your post meta '_thumbnail_id' for post as well.

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

这篇关于通过App上传时,JSON API Wordpress不显示特色图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆