Skip to content

Instantly share code, notes, and snippets.

@yuliyang
Created May 18, 2023 10:34
Show Gist options
  • Save yuliyang/9308a3c694647b5a2d1a4c1196f9fa7c to your computer and use it in GitHub Desktop.
Save yuliyang/9308a3c694647b5a2d1a4c1196f9fa7c to your computer and use it in GitHub Desktop.
[ WooCommerce ] Remove featured image/first image from product gallery
<?php
// remove featured image from product gallery
add_filter('woocommerce_single_product_image_thumbnail_html', 'ds_remove_product_featured_image', 10, 2);
function ds_remove_product_featured_image($html, $attachment_id ) {
global $post, $product;
$featured_image = get_post_thumbnail_id( $post->ID );
if ( $attachment_id == $featured_image )
$html = '';
return $html;
}
// remove first image from product gallery
add_filter( 'woocommerce_product_gallery_attachment_ids', 'ds_filter_product_gallery', 10, 2 );
function ds_filter_product_gallery( $ids, $product ){
if (is_product()) {
set_post_thumbnail( $post, $ids[0] );
unset($ids[0]);
}
else {
set_post_thumbnail( $post, end($ids));
}
return $ids;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment