WordPress (Woocommerce) get second image attached to product

I’ve searched everywhere for this and didn’t find the help I needed but I thought this would have been a common query.

I’m building a WooCommerce theme and in the default product category view each product displays a front image and a back image appears when one hovers over the product. My desire is to have the second image uploaded to the product as the source of the back image. I understand I could use a custom field or a plugin to create and get a second featured image but I’m trying to avoid bloating WordPress with unnecessary scripts.

I’m using this to get the featured product image:

   <?php if (has_post_thumbnail( $loop->post->ID )){

            $img_src_array = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single_product_large_thumbnail_size' );
            $img_src = $img_src_array[0];

            echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
        }
    ?>

And it’s working perfectly.

I tried using the method here: http://www.rlmseo.com/blog/get-images-attached-to-post but it returned one product image for all products in the loop instead of only returning images attached to particular products.

This one wouldn’t work either: http://wordpress.org/support/topic/retreive-first-image-from-post?replies=19, it failed to echo the first image attached to the product, never mind the second.

Could it be that WooCommerce product image attachments are different from the default WordPress ones? Please can someone assist me and explain (even if only with a few words) what they’re doing. I thought I was getting to know WordPress but I’ve been completely noobified by this relatively simple function.

Read more here: WordPress (Woocommerce) get second image attached to product

Leave a Reply

Your email address will not be published. Required fields are marked *