gpt4 book ai didi

php - 使用 Hook 替换 Woocommerce 商店页面上的图像

转载 作者:行者123 更新时间:2023-12-04 13:41:30 25 4
gpt4 key购买 nike

我正在尝试使用 Hook 从 Woocommerce 的商店/目录页面中删除产品缩略图,然后将其替换为我自己的自定义图像。

add_action 按预期工作并显示文本,但 remove_action 不会删除产品缩略图。 Here is the page related web site page.

我做错了什么?

下面是我使用的代码:

// 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";
}
}

最佳答案

您只需要使用 woocommerce_init Hook 启动它,这样它就可以正常工作。

代码如下:

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');

此代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

此代码已经过测试且功能齐全。

关于php - 使用 Hook 替换 Woocommerce 商店页面上的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39758471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com