Tudor
Tudor

Reputation: 37

Woocommerce conditional featured images & separate gallery in the single product loop

Initial disclaimer, I have scoured SO and Google for ways to do this, extensively and exhaustively, to no avail. Please don't post links to other questions, I have already tried them all.

I've reduced the problem to a concept, to see if it's even possible to achieve in a sensible manner.

So this is my problem. A WooCommerce product has:

  1. A product image (~a featured image)
  2. A product gallery (with a lightbox)

What I'd like to achieve:

  1. Ideally, multiple featured images based on the archive (product grid) page they are shown. (i.e. blue tshirt on blue clothes page, red tshirt variation of the same product on red clothes page).
  2. The gallery in the single page, without the featured images in the same loop.

See attached images for a more visual idea. Essentially I'd like to have conditional featured images & a product gallery without featured images in the loop.

How would I go about doing that? Thank you so much in advance.

enter image description here

Edit 1: I managed to write the code for the gallery without the featured images:

<?php
    global $product;
    $product_id=$product->id;
    $attachment_ids = $product->get_gallery_attachment_ids();

    foreach( $attachment_ids as $attachment_id ) {
        // Display the image URL
        echo $Original_image_url = wp_get_attachment_url( $attachment_id );

        // Display Image instead of URL
        echo wp_get_attachment_image($attachment_id, 'full');
    }
?>

So now all I need is conditional featured images based on the page ID?

Upvotes: 1

Views: 1053

Answers (1)

Tudor
Tudor

Reputation: 37

Ended up using Advanced Custom Fields for the separate featured images, all I need now is to conditionally show them based on page ID.

Upvotes: 0

Related Questions