Zet*_*eta 1 wordpress woocommerce
如何在 WooCommerce 中为产品分配多个图像?
我试过了:
update_post_meta( $post_id, '_product_image_gallery', $image_id);
Run Code Online (Sandbox Code Playgroud)
但它只分配一个图像。当我使用数组时,它不起作用:
update_post_meta( $post_id, '_product_image_gallery', array($image_id,$image_id2));
Run Code Online (Sandbox Code Playgroud)
如果您有多个图像需要分配给一个产品,您需要将一个图像分配为特色图像/缩略图,然后将其余图像分配为产品图库缩略图。
以下是可以为您实现此目的的快速功能:
function so_upload_all_images_to_product($product_id, $image_id_array) {
//take the first image in the array and set that as the featured image
set_post_thumbnail($product_id, $image_id_array[0]);
//if there is more than 1 image - add the rest to product gallery
if(sizeof($image_id_array) > 1) {
array_shift($image_id_array); //removes first item of the array (because it's been set as the featured image already)
update_post_meta($product_id, '_product_image_gallery', implode(',',$image_id_array)); //set the images id's left over after the array shift as the gallery images
}
}
Run Code Online (Sandbox Code Playgroud)
假设您有一个图像 ID 数组,以及要附加图像的产品 ID,您可以像这样调用上面的函数:
$images = array(542, 547, 600, 605); //array of image id's
so_upload_all_images_to_product($product_id, $images);
Run Code Online (Sandbox Code Playgroud)
如果您正在处理大量产品图像,或者您非常重视微优化,则可以使用array_reverse和array_pop的组合,而不是array_shift。
| 归档时间: |
|
| 查看次数: |
4837 次 |
| 最近记录: |