我正面临一些未知的问题与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";
Run Code Online (Sandbox Code Playgroud)
任何有关这方面的帮助将受到高度赞赏.
截图
我不确定您的代码,但我的下面的代码可以完美地将图像指定为帖子的特征图像。请看一下。附件 ID 应分配给具体帖子,请检查您的帖子元“_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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
828 次 |
| 最近记录: |