Remove featured image from the WooCommerce gallery

百般思念 提交于 2019-12-04 01:24:46

问题


I tried using suggestions from other posts but they do not work. I am looking to not show the featured product image in my product images area/image gallery because I am using the featured image in the header as a background image and its too wide to be in the gallery.

So far this is the closest thing to working but I get an error. It does however do what I need.

Any way to fix this so i do not get the error?

Here is my code:

add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 3);
function remove_featured_image($html, $attachment_id, $post_id) {
    $featured_image = get_post_thumbnail_id($post_id);
    if ($attachment_id != $featured_image) {
        return $html;
    }
    return '';
}

And here is the error:

Missing argument 3 for remove_featured_image() in /home/…/plugins/my-custom-functions/inc/php/functional.php(93) : eval()'d code on line 4

Warning: Missing argument 3 for remove_featured_image() in /home…/plugins/my-custom-functions/inc/php/functional.php(93) : eval()'d code on line 4


回答1:


There is only 2 arguments for woocommerce_single_product_image_thumbnail_html filter hook.

So you have to change a little bit your code to avoid the error, this way:

add_filter('woocommerce_single_product_image_thumbnail_html', 'remove_featured_image', 10, 2);
function remove_featured_image($html, $attachment_id ) {
    global $post, $product;

    $featured_image = get_post_thumbnail_id( $post->ID );

    if ( $attachment_id == $featured_image )
        $html = '';

    return $html;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.


References for filter hook woocommerce_single_product_image_thumbnail_html:

  • woocommerce templates: single-product/product-image.php
  • woocommerce templates: single-product/product-thumbnails.php


来源:https://stackoverflow.com/questions/44732859/remove-featured-image-from-the-woocommerce-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!