Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save huntlyc/65568f414ae8b8d50017 to your computer and use it in GitHub Desktop.
Save huntlyc/65568f414ae8b8d50017 to your computer and use it in GitHub Desktop.
Wraps woocommerce product images - based on Bradley D's gist: https://gist.github.com/Bradley-D/9479200
<?php
/**
* wrapWoocommerceImages()
*
* Wraps woocommerce product images with spans making it easier to apply custom styles.
*
* This is a slightly more involved approach based on this gist:
* https://gist.github.com/Bradley-D/9479200
*
* Example markup after using this function:
*
* <a href="{$productURL}">
* <span class="woocommerce-img-wrap {$extraClasses}">
* <img src="{$imgSrc}" class="attachment-shop_catalog size-shop_catalog wp-post-image" alt="{$productTitle}">
* </span>
* <h3>{$productTitle}</h3>
* </a>
*
*/
function wrapWoocommerceImages(){
/*
* Woocommerce Image Hooks
*
* Add all hooks in here, with an array of additional classes you wish
* to add to the wrap
*/
$imgHooks = array(
'woocommerce_before_subcategory_title' => array('sc-img'),
'woocommerce_before_shop_loop_item_title' => array(),
);
if(!empty($imgHooks)){
//Process all the hooks
foreach ($imgHooks as $hook => $extraClasses) {
//Turn our extra classes array into a space seperated list
$extraClasses = implode(' ', $extraClasses);
//The "Before" img action
add_action( $hook, function() use ($extraClasses){
echo "<span class=\"woocommerce-img-wrap $extraClasses\">";
} , 5, 2);
//The "after" img action
add_action( $hook, function(){
echo '</span>';
}, 12, 2);
}
}
}
wrapWoocommerceImages();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment