John Davidson

php - Do not show the thumbnail row on Woocommerce product page if the product only has 1 image available

0 comments
Message:


In Woocommerce, I have products that contain 1 image (featured image) and other products that have the main featured image as well as additional images added in the gallery.


On the product page, how can I hide the thumbnail row that is display just under the main image but for products that only have 1 image and in the same time keep showing the row in case the product has multiple images.


When viewing a product that has only the main featured image added with no other images in the gallery the product page displays the main image as well as a small thumbnail of it in the thumbnail zone below it. I find this is redundant and the thumb is not needed if it's the only image available. Especially on mobile it looks awkward to display the main image and the same image below it as a thumb


Thank you



My Answer:
You can achieve this by adding the following code to your theme's functions.php file:

php
add_action( 'wp', 'hide_thumbnail_row_if_single_image' );

function hide_thumbnail_row_if_single_image() {
global $product;

if ( $product && $product->get_image_count() == 1 ) {
remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
}
}


This code checks if the product has only one image using the `get_image_count()` method. If it does, it removes the action that displays the thumbnail row using `remove_action()`. This way, the thumbnail row will only be displayed for products with multiple images in the gallery.

Rate this post

5 of 5 based on 9762 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us