简体   繁体   English

使用挂钩替换Woocommerce商店页面上的图像

[英]Replacing Image on Woocommerce Shop Page using hooks

I'm trying to use hooks to remove the product thumbnail from the shop/catalog page in Woocommerce and then replace it with my own custom image. 我试图使用挂钩从Woocommerce的商店/目录页面中删除产品缩略图,然后将其替换为我自己的自定义图片。

add_action works as expected and displays the text, but the remove_action does not remove the product thumbnail. add_action可以正常工作并显示文本,但是remove_action不会删除产品缩略图。 Here is the page related web site page. 这是与页面相关的网站页面。

What I am doing wrong? 我做错了什么?

Below is the code I'm using: 以下是我正在使用的代码:

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

//Add custom code to replace product thumbnail
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumb', 10 );

if ( ! function_exists( 'woocommerce_template_loop_product_thumb' ) ) {
    function woocommerce_template_loop_product_thumb() {
        echo "testing";
    }
}

You just need to fire this using woocommerce_init hook and this way it just works. 您只需要使用woocommerce_init钩子触发它,这样就可以正常工作。

Here is the code: 这是代码:

function replacing_template_loop_product_thumbnail() {
    // Remove product images from the shop loop
    remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
    // Adding something instead
    function wc_template_loop_product_replaced_thumb() {
        echo "TEST TEST";
    }
    add_action( 'woocommerce_before_shop_loop_item_title', 'wc_template_loop_product_replaced_thumb', 10 );
}
add_action( 'woocommerce_init', 'replacing_template_loop_product_thumbnail');

This code goes in function.php file of your active child theme (or theme) or also in any plugin file. 这段代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中。

This code is tested and fully functional. 此代码已经过测试,功能齐全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM