On a collection page, I would like the 2nd image to show but only on products with certain tags

Is there a way for the second image to show for a product with a specific tag on a collection page, without changing the order of the images for that particular product.

Basically, I tried this little conditional code below, but it changes all of the images on the collection page to the second one:

{% if product.tags contains ‘secondimage’ %}

.product-item .first { opacity: 0; } .product-item .not-first { opacity: 1; }

{% endif %}