Woocommerce – Featured Image URL and Gallery images

Nov 28, 2014
admin

Get Woocommerce Gallery Images :

                    <?php  global $product;
                         $attachment_ids = $product->get_gallery_attachment_ids();
                        foreach( $attachment_ids as $attachment_id ) 
                        {
                          echo $image_link = wp_get_attachment_url( $attachment_id );
                        }    ?>

Woocommerce Product Featured Image:

<?php if ( has_post_thumbnail( $product->id ) ) {
                        $attachment_ids[0] = get_post_thumbnail_id( $product->id );
                         $attachment = wp_get_attachment_image_src($attachment_ids[0], 'full' ); ?>    
                        <img src="<?php echo $attachment[0] ; ?>" class="card-image"  />
                    <?php } ?>

var_dump($attachment_ids)

 array (size=4)
  0 => string 'http://localhost/wp-content/uploads/2014/11/bottle.png' (length=64)
  1 => int 350
  2 => int 500
  3 => boolean false

12 Comments. Leave new

Vishal Kakadiya
June 16, 2016 11:14 am

Hello,

Please put $attachment[0] in src attribute of the img tag in Woocommerce Product Featured Image.

So the new <img src="” class=”card-image” />

Reply
Vishal Kakadiya
June 16, 2016 11:16 am

Change above thing and your code is excellent !

Thanks (Y)

Reply

Updated! Thanks!

Reply
Robert Revx
May 28, 2017 12:21 pm

Hey. Thanks for the post, what must I do to understand how to apply this? Do I define it as a function and call the function from each post? I’m looking at the code and I’m lost! How does it get the img src? Please point me in a few articles I should read because I don’t know what it’s called that I’m looking for. I just want to learn to apply this.

Reply

hello sorry for this code where it is copied to be able to have this feature?

Reply

You can use these snippets anywhere in your template if you declare wanted product ($product->id)….otherwise, it’s intended to be used on single-product layout.

Reply

i want to show wc_additional_variation_images_gallery_images
no Product gallery
how can i do this?

Reply

Hi, this was really helpful, thanks. Could you tell me how I can add the Product Featured Image “alt” tag as well?

Reply

Well I figured it out a solution myself: alt=”‘ . esc_attr( get_the_title( $post_id ) ) . ‘”

Thanks again for this article

Reply

I am adding this code in template.php but the featured image is not showing in post background.

$blog_fetrdimg = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div class="blog-featured-image"

style=”blog-featured-background-image: url();”

Here I have seen this code [..hidden..]

Reply

There are some issues in code you are trying to use:

<div id="custom-bg" style="wpblog-featured-image: url('<?php echo $image[0]; ?>')">

should be:

<div id="custom-bg" class="wpblog-featured-image" style="background-image: url('<?php echo $image[0]; ?>')">
Reply

Leave a Reply

Your email address will not be published. Required fields are marked *