[Resolved] Woocommerce image from gallery as header image

Home Forums Support [Resolved] Woocommerce image from gallery as header image

Home Forums Support Woocommerce image from gallery as header image

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #830872
    Krzysztof

    Hello I am working currently on a project which requires me on single product page to display big image which is always first in gallery added to said product. Found the code on web which seems to get things going, but I can’t figure out why it requests images which are not first from woo gallery attached to the product.
    Here is the snippet with shortcode:

    function gal_first_image_shortcode( $atts, $content = null ) {
       extract( shortcode_atts( array(
          'size' => 'shop_single'
          ), $atts ) );
      
        $images = get_children(array(
            'post_parent' => get_the_ID(),
            'post_type' => 'attachment',
            'numberposts' => 1,
            'post_mime_type' => 'image',
            'orderby' => 'menu_order',
            )
        );
      
        if($images) {
            $gallery = '';
            foreach( $images as $image ) {
                        $gallery .= wp_get_attachment_image($image->ID, $image_size);
            }
            $gallery .= '';
      
            return $gallery;
        }
      
    }
    add_shortcode('woo_first_gal', 'gal_first_image_shortcode');

    It does return image, but not first when looking at the attached gallery table.

    #831096
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    I’m not 100% sure, but you should be able to do something like this:

    $product = wc_get_product( get_the_ID() );
    
    $images = $product->get_gallery_image_ids();
    
    $first = $images[0];
    
    $url = wp_get_attachment_url( $first );
    #831809
    Krzysztof

    Hey Tom, thanks for reply. So I should just dump my entire code and replace it with Yours?
    Actually, it works in this way:

    function gal_first_image_shortcode( $atts, $content = null ) {  
    $product = wc_get_product( get_the_ID() );
    
    $images = $product->get_gallery_image_ids();
    
    $first = $images[0];
    
    $url = wp_get_attachment_image( $first, $size='shop_single');
    
     return $url;
    }
    add_shortcode('woo_first_gal', 'gal_first_image_shortcode');

    Thanks a lot!

    #832258
    Tom
    Lead Developer
    Lead Developer

    You’re welcome 🙂

    #2263173
    Benny

    I used the code provided above and it works just fine but is there way to make it a gallery with dots to scroll the images?

    #2263226
    David
    Staff
    Customer Support

    Hi there,

    you would need to incorporate slider script of some kind to do that, which is out of our scope.
    Might be worth googling to see if theres a plugin that already does this.

    #2263400
    Benny

    Ok, so i bought this one –> https://woocommerce.com/de-de/products/product-gallery-slider-for-woocommerce/

    But there is one little thing, i need help with. I would like it mobile to be fullwidth. Maybe you see it here –> http://avantal.axmxvonbiw-v1p3z0dkj6ye.p.runcloud.link/milbentest/

    #2263458
    Leo
    Staff
    Customer Support

    This is something you will need to check with the plugin’s support team.

    Thanks!

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.