Skip to content

Instantly share code, notes, and snippets.

@carasmo
Last active December 22, 2017 18:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save carasmo/261fbba1b422188bb1bd79451544ac0b to your computer and use it in GitHub Desktop.
Save carasmo/261fbba1b422188bb1bd79451544ac0b to your computer and use it in GitHub Desktop.
Two different simple ways to wrap WooCommerce's image in the archive loop without recreating the loop or the image. The first one is jQuery, which will work if you are NOT using infinite scrolling, load more, endless page, whatever. The next code is the php version which works all the time.
//jQuery version
( function( window, $, undefined ) {
'use strict';
$( document ).ready( function( ) {
//IMPORTANT: intended to be added to your existing doc ready so don't repeat this stuff above
/**
* Wrap Product Archive Loop Image WooCommerce
* https://ChristinaCreativeDesign.com
*/
$( 'ul.products img' ).addClass( 'pretty-wrap' );
$( 'img.pretty-wrap' ).wrap( '<span class="my-class-name"/>' );
//IMPORTANT: intended to be added to your existing doc ready so don't repeat this stuff below
} ); //* end doc ready
} )( this, jQuery );
<?php
// don't add to your functions.php file, it is already there
/**
* Wrap Archive Product Loop Image WooCommerce
* https://ChristinaCreativeDesign.com
*/
function yourprefix_wrap_loop_product_image() {
if ( ! class_exists( 'WooCommerce' ) ) return; //* exit early if WooCommerce not active/installed
add_action( 'woocommerce_before_shop_loop_item_title' , 'yourprefix_product_loop_image_wrapper_open', 9 );
add_action( 'woocommerce_shop_loop_item_title' , 'yourprefix_product_loop_image_wrapper_close', 9 );
}
add_action( 'woocommerce_before_shop_loop' , 'yourprefix_wrap_loop_product_image', 3 );
//open my-class-name
function yourprefix_product_loop_image_wrapper_open() {
echo '<span class="my-class-name">';
}
//open my-class-name
function yourprefix_product_loop_image_wrapper_close() {
echo '</span>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment