WooCommerce: Change No. of Thumbnails per Row @ Product Gallery

WooCommerce 3.0 completely revolutionized the single product page featured and product gallery images, including their PHP. If, in the past, a simple filter from WooCommerce was enough to change the number of thumbnails per row (https://docs.woocommerce.com/document/change-number-of-thumbnails-per-row-in-product-galleries/), this does not exists anymore ๐Ÿ™

So, let’s see how we can achieve this in WooCommerce 3.0 and above. For this example we’ll use two themes – in fact some theme-specific CSS is also needed together with the PHP.

WooCommerce & 2017 Theme: change number of thumbs per row in the Product Gallery

PHP Snippet: Change No. of Thumbnails per Row @ Product Gallery | WooCommerce

/**
 * @snippet       Change No. of Thumbnails per Row @ Product Gallery
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 5.0
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_single_product_image_gallery_classes', 'bbloomer_5_columns_product_gallery' );

function bbloomer_5_columns_product_gallery( $wrapper_classes ) {
   $columns = 5; // change this to 2, 3, 5, etc. Default is 4.
   $wrapper_classes[2] = 'woocommerce-product-gallery--columns-' . absint( $columns );
   return $wrapper_classes;
}

Unfortunately this filter is usually not sufficient as WordPress themes apply their own CSS to the new Product Gallery. So, let’s see what else is needed to make it all work.

CSS to be added for TwentySeventeen Theme (Change No. of Thumbnails per Row @ Product Gallery)

Please note: this blog’s featured image is the result of PHP + CSS on the TwentySeventeen Theme.

/* Remove default "clear" at position 5, 9, etc. This is for 4 columns */

.woocommerce-product-gallery .flex-control-thumbs li:nth-child(4n+1) {
    clear: none;
}

/* Add new "clear" at position 6, 11, etc. This is for 5 columns */

.woocommerce-product-gallery .flex-control-thumbs li:nth-child(5n+1) {
    clear: left;
}

CSS to be added for Storefront Theme (Change No. of Thumbnails per Row @ Product Gallery)

/* Add new CSS for 5 columns */

.single-product div.product .woocommerce-product-gallery.woocommerce-product-gallery--columns-5 .flex-control-thumbs li {
    width: 11.1%;
    margin-right: 11.1%;
float: left;
}

.single-product div.product .woocommerce-product-gallery.woocommerce-product-gallery--columns-5 .flex-control-thumbs li:nth-child(5n) {
    margin-right: 0;
}

Final result:

Change WooCommerce Product Gallery to 5 Columns on Storefront Theme

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]
  • WooCommerce: Show Number Of Products Sold @ Product Page
    WooCommerce database already stores the number of products sold for you. Therefore, you may want to show such number on the product page, close to the Add To Cart button. As we’ve seen in my book Ecommerce and Beyond, showing the number of sales for each product can increase your sales conversion rate. All you […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

15 thoughts on “WooCommerce: Change No. of Thumbnails per Row @ Product Gallery

  1. Hi there,

    the code work well on desktop / tablet but not for mobile devices.
    I set 5 column but on mobile i still have 4 columns.

    Can you help me?

    1. Hi there, it must be your theme or a different plugin interfering with that. It will require custom troubleshooting. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Hello, I just fixed that Firefox width issue by overwriting the minified jquery file from woocommerce/assets/js/flexslider/jquery.flexslider.min.js

    I changed this : `m.isFirefox&&(m.w=m.width())`
    To this : `m.isFirefox&&(m.w=m.viewport())`

    It will use the width of the **viewport** in place of the entire slider.

    Wish this can help! Its worked perfectly for me as i use the thumbmails on left side.

  3. Thank you! This helped me with a very quick fix to remove the line breaks with images. Much appreciated.

  4. Hi Rodolfo, excellent tip.

    Another way it’s to hook before woocommerce_single_product_image_gallery_classes and return the number of columns that also generate the correct data-columns number in the div (with the correct class); something like this works fine:

    /**
     * Change Single product photo gallery number of columns.
     */
    add_filter(
    	'woocommerce_product_thumbnails_columns',
    	function() {
    		return 7;
    	}
    );
    
      1. I just wanted to drop in and say thank you, Carlos!

  5. I need 10 columns.. how should i adjust it? thanks

    1. Hi Dale, you would put 10 instead of 5, and then based on your theme you may also need some additional custom CSS

  6. In Customizr Pro You only have to change CSS without adding any changes to functions.php:

    /* remove clear for every 4th image */
    .woocommerce div.product .woocommerce-product-gallery--columns-4 .flex-control-thumbs li:nth-child(4n+1) {
    clear: none;
    }
    
    /* add clear for every 6th image (change for yourself) */ 
    .woocommerce div.product .woocommerce-product-gallery--columns-4 .flex-control-thumbs li:nth-child(6n+1) {
    clear: left;
    }
    
    /* Resize image, for default is 25%, for 5 is 20%, for 6 is 16.666%, etc. */
    .woocommerce div.product div.images .flex-control-thumbs li {
    width: 16.666%;
    }
    
    1. Cool, but you will also need to add responsive CSS right? ๐Ÿ™‚

  7. snippet did not work for me, using Genesis Frameworks theme. My client has 1 product with only 2 thumbs that she wants centered under the main photo, I was hoping this would help but no changes are seen.

    1. Thanks for your comment Jan! I guess Genesis has its own code so the snippet would need to be modified slightly ๐Ÿ™‚

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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