Remove featured image from the WooCommerce gallery

2019-05-03 19:20发布

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条回答
一夜七次
2楼-- · 2019-05-03 19:57

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:

查看更多
登录 后发表回答