我正在尝试通过php更改产品图片。但我无法让它发挥作用。
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$yesterday = date("Y-m-d", time() - 60 * 60 * 24);
$jsondata = file_get_contents("730.json");
ini_set('memory_limit', '-1');
$json = json_decode($jsondata, true);
$price = $json["AWP | Asiimov (Well-Worn)"][$yesterday]["price"]/100;
$post_information = array(
'post_author' => $user_id,
'post_content' => '',
'post_status' => "publish",
'post_title' => 'test',
'post_parent' => '',
'post_type' => "product",
);
$image = media_sideload_image("http://www.technologijos.lt/upload/image/n/technologijos/it/S-49657/samsung-ssd-840-evo.jpg", 0, "The WordPress Logo");
$post_id = wp_insert_post($post_information);
$attachment = wp_get_attachment_image( 18, 'thumbnail' );
update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, 'total_sales', '0');
update_post_meta( $post_id, '_downloadable', 'no');
update_post_meta( $post_id, '_virtual', 'yes');
update_post_meta( $post_id, '_regular_price', $price );
update_post_meta( $post_id, '_sale_price', "1" );
update_post_meta( $post_id, '_purchase_note', "" );
update_post_meta( $post_id, '_featured', "no" );
update_post_meta($post_id, '_sku', "145");
update_post_meta( $post_id, '_product_attributes', array());
update_post_meta( $post_id, '_sale_price_dates_from', "" );
update_post_meta( $post_id, '_sale_price_dates_to', "" );
update_post_meta( $post_id, '_price', $price );
update_post_meta( $post_id, '_sold_individually', "" );
update_post_meta( $post_id, '_manage_stock', "no" );
update_post_meta( $post_id, '_backorders', "no" );
update_post_meta( $post_id, '_stock', "" );
update_post_meta( $post_id, '_product_image_gallery', $attachment );
最后一行无效。我不知道如何让它工作。我尝试改变不同的东西,(我是PHP / WordPress的新手)。
答案 0 :(得分:2)
您是否尝试设置精选图片?
如果是这样,您可能想要使用answer here中给出的功能。
功能:
function Generate_Featured_Image( $image_url, $post_id ){
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
if(wp_mkdir_p($upload_dir['path'])) $file = $upload_dir['path'] . '/' . $filename;
else $file = $upload_dir['basedir'] . '/' . $filename;
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
$res1= wp_update_attachment_metadata( $attach_id, $attach_data );
$res2= set_post_thumbnail( $post_id, $attach_id );
}
<强>用法:强>
Generate_Featured_Image( '../wp-content/my_image.jpg', $post_id );
// $post_id is Numeric ID... You can also get the ID with: wp_insert_post()
答案 1 :(得分:0)
我想补充一下bryceadams的答案。
似乎就是这样,例如,eBay的Finding API会返回其URL具有不允许命令的结构的图像
$filename = basename($image_url);
按预期运行,因为发生的情况是,图像的所有URL都具有相同的结束基名,并且文件会发生冲突。我发现了命令
$filename = basename($image_url, '.jpg') . (string) rand(0, 5000) . '.jpg';
适用于许多API。希望它能增加并提供帮助。