The output of an product image on Woocommerce shop and archive pages looks like:
<img src="http://localhost/wp-content/uploads/2022/02/long-sleeve-tee-2-300x300.jpg" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail" alt="" loading="lazy" srcset="http://localhost/wp-content/uploads/2022/02/long-sleeve-tee-2-300x300.jpg 300w, http://localhost/wp-content/uploads/2022/02/long-sleeve-tee-2-100x100.jpg 100w, http://localhost/wp-content/uploads/2022/02/long-sleeve-tee-2-500x500.jpg 500w, http://localhost/wp-content/uploads/2022/02/long-sleeve-tee-2-150x150.jpg 150w, http://localhost/wp-content/uploads/2022/02/long-sleeve-tee-2-768x768.jpg 768w, http://localhost/wp-content/uploads/2022/02/long-sleeve-tee-2.jpg 801w" sizes="(max-width: 300px) 100vw, 300px" width="300" height="300">
I would like to add classes. I've searched everywhere I can think for class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail"
with no luck.
I tried adding the classes with jQuery:
<script>
'use strict';
( function( $ ) {
<?php if (is_shop()): ?>
// Add classes to the img tag
$('.attachment-woocommerce_thumbnail').addClass('w-full md:h-96 sm:h-auto object-cover');
<?php else: ?>
$('.attachment-woocommerce_thumbnail').addClass('w-full object-cover 2md:h-96');
<?php endif ?>
} )(jQuery);
</script>
Which, works at first, but I use a infinite scroll plugin someone else wrote, and it does not add the classes to the new images when the infinite scroll plugin loads more images. Any advice?
You can use the
wp_get_attachment_image_attributes
WordPress filter hook, which will allow you to add the desired class via$attr['class']
I have added 3 examples:
So you get: