I need to woocommerce product gallery add class in latest item, if more than two pictures in the gallery do no need add the class to the last element, but if there are more than two images in the gallery - starting from three, need add the class to the last item, i have this code, code working and in latest item i can add class, but if less 3, dont know how:

              global $product;
                      $attachment_ids = $product->get_gallery_attachment_ids();
              $count = count($attachment_ids); $num = 0;
              foreach( $attachment_ids as $attachment_id ) {
                  $thumbnail_url = wp_get_attachment_image_src( $attachment_id, 'full' )[0];?>
              <li class="<?php if($num == $count-1){ ?><?php }else{ ?>small<?php }?>"><a class="product-images__list-item" href="<?php echo $thumbnail_url; ?>"><img src="<?php echo $thumbnail_url; ?>"></a></li>
              <?php $num++;?>
              <?php } ?>
  • I'm not sure I understand you well, but to add small class if there are more than two elements: <li class="<?php echo ($num == $count - 1 && $count > 2) ? 'small' : ''; ?>">
    – nmr
    Apr 15, 2019 at 14:22
  • thank you, working properly
    – Lerry
    Apr 15, 2019 at 15:10


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.