Skip to content

Instantly share code, notes, and snippets.

@webdevs-pro
Created September 17, 2019 04:10
Show Gist options
  • Save webdevs-pro/fe04d4552920f803a9e64a7a713a94db to your computer and use it in GitHub Desktop.
Save webdevs-pro/fe04d4552920f803a9e64a7a713a94db to your computer and use it in GitHub Desktop.
set woo product fatured image by product first gallery image
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$product_id = get_the_ID();
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_attachment_ids();
echo $attachment_ids[0];
echo wp_get_attachment_image($attachment_ids[0], 'thumbnail');
echo '<br>';
// if ( $attachment_ids[0] ) {
// set_post_thumbnail( get_the_ID(), $attachment_ids[0] );
// }
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment