P粉0116843262023-08-27 18:00:39
You can first check if get_gallery_image_ids
returns an array. If it exists, check if key 0 (the first element) exists. If so, then you're free to use it however you want.
... // Get all IDs $idList = wc_get_product()->get_gallery_image_ids(); // Check if the IDs are an array and key 0 (first element) exists if (is_array($idList) && array_key_exists(0, $idList)) { // Get the first element $image_id = $idList[0]; echo wp_get_attachment_image($image_id, 'woocommerce_thumbnail' ) ; } else { echo wp_get_attachment_image(wc_get_product()->get_image_id(), 'woocommerce_thumbnail' ) ; }
edit,
You should edit your mem_add_on_hover_shop_loop_image
function with this code. The final code should look like this,
add_action('woocommerce_before_shop_loop_item_title', 'mem_add_on_hover_shop_loop_image'); function mem_add_on_hover_shop_loop_image() { // Get all IDs $idList = wc_get_product()->get_gallery_image_ids(); // Check if the IDs are an array and key 0 (first element) exists if (is_array($idList) && array_key_exists(0, $idList)) { // Get the first element $image_id = $idList[0]; echo wp_get_attachment_image($image_id, 'woocommerce_thumbnail'); } else { echo wp_get_attachment_image(wc_get_product()->get_image_id(), 'woocommerce_thumbnail'); } }