首页 文章

WooCommerce;从商店页面和单个产品页面删除产品图像

提问于
浏览
0

我试图从WooCommerce的商店页面和单一产品页面有条件地删除产品图像 . 它正在从单个产品中删除图像,而不是/ shop页面上的产品 .

//* Conditionally remove the Featured Product Image from the single-product page
function remove_gallery_and_product_images() {
if ( is_product() && is_single(array(1092, 1093, 1094) ) ) {
  remove_action( 'woocommerce_before_single_product_summary', 
'woocommerce_show_product_images', 20 );
  add_filter('body_class', 'no_prod_imgs_class');
  }
}
  add_action('template_redirect', 'remove_gallery_and_product_images');

//* Add CSS for removed featured images from multiple specific product detail 
pages
function no_prod_imgs_class($classes) {
$classes[] = 'no-product-images';
return $classes;
}

4 回答

  • 1

    删除所有单个产品页面上的所有缩略图图像

    将此代码添加到您的子主题functions.php文件中 .

    remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
    
    // Remove product images from the shop loop
    remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
    
  • 0

    这是用于删除单个产品图像的过滤器 .

    function remove_single_product_image( $html, $thumbnail_id ) {
        return '';
    }
    
    add_filter( 'woocommerce_single_product_image_thumbnail_html', 'remove_single_product_image', 10, 2 );
    

    以下是删除商店页面缩略图的代码 .

    function remove_woocommerce_actions() {
        remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
    }
    
    add_action( 'after_setup_theme', 'remove_woocommerce_actions' );
    
  • 1

    非常感谢您的回答 . 我之前找到了那些删除操作,他们从产品或/ shop页面删除了图像,但我无法定位特定的产品ID;无论出于何种原因,此删除操作都不希望您定位特定的产品ID!事实证明,一个有效的解决方案是针对特定的页面ID . 换句话说,在我的情况下,我会在没有产品图像的存档页面上使用此产品,但是另一个产品存档页面将包含产品图像 .

    // Conditionally remove product images from the shop loop
    function remove_product_image_conditionally() {
      if ( is_page( 1108 ) ) {
        remove_action( 'woocommerce_before_shop_loop_item_title', 
        'woocommerce_template_loop_product_thumbnail', 10 );
      }
    }
      add_action('template_redirect', 'remove_product_image_conditionally');
    
  • 0

    您可以使用这些代码来实现您的目标:

    // Remove image from product pages
    remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
    
    // Remove sale badge from product page
    remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );
    

    使用上面的代码删除单个产品页面上的woocommerce图像 . 请注意,对于待售产品,第二个操作会删除单个产品页面上的销售徽章 .

    然后要删除商店页面的woocommerce图像,请使用以下操作:

    // Remove product images from the shop loop
    remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
    
    
    // Remove sale badges from the shop loop
    remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
    

    就像之前这些行动将删除商店页面的woocommerce图像和销售徽章 .

相关问题