通过Woocommerce中的钩子定制循环产品图像 [英] Customizing loop product image via a hook in Woocommerce
问题描述
我正在自定义woocommerce主题.我使用钩子动作woocommerce固定在循环产品上.
I am customizing woocommerce themes. I stuck on loop product using hook action woocommerce.
要在循环中调用/包含缩略图,我们将其称为钩子
To call/include the thumbnail image in a loop, we call this hook
<?php do_action('woocommerce_before_shop_loop_item_title'); ?>
并且出现缩略图.我很困惑< img src" ....
的位置在哪里?如何编辑该代码?
And the thumbnail image appears.
I am confused where is the <img src"" ....
location?
How to edit that code?
谢谢
推荐答案
钩子 woocommerce_before_shop_loop_item_title
从 此功能代码 :
if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
/**
* Get the product thumbnail for the loop.
*/
function woocommerce_template_loop_product_thumbnail() {
echo woocommerce_get_product_thumbnail(); // WPCS: XSS ok.
}
}
因此您可以看到它使用>> woowoo_get_product_thumbnail()函数:
So as you can see it uses woocommerce_get_product_thumbnail()
function for it:
if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {
/**
* Get the product thumbnail, or the placeholder if not set.
*
* @param string $size (default: 'woocommerce_thumbnail').
* @param int $deprecated1 Deprecated since WooCommerce 2.0 (default: 0).
* @param int $deprecated2 Deprecated since WooCommerce 2.0 (default: 0).
* @return string
*/
function woocommerce_get_product_thumbnail( $size = 'woocommerce_thumbnail', $deprecated1 = 0, $deprecated2 = 0 ) {
global $product;
$image_size = apply_filters( 'single_product_archive_thumbnail_size', $size );
return $product ? $product->get_image( $image_size ) : '';
}
}
我希望这能回答您的问题,并消除您的困惑.
I hope that this answers your question, and removes your confusion.
自定义循环商品图片
现在,您可以使用以下方法从钩子中删除此默认功能,以添加自己的自定义功能:
Now, you can remove this default function from the hook to add your own custom one, using this:
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
add_action( 'woocommerce_before_shop_loop_item_title', 'custom_loop_product_thumbnail', 10 );
function custom_loop_product_thumbnail() {
global $product;
$size = 'woocommerce_thumbnail';
$image_size = apply_filters( 'single_product_archive_thumbnail_size', $size );
echo $product ? $product->get_image( $image_size ) : '';
}
代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
所以现在您只需要自定义函数中的代码...
So now you just need to customize the code inside the function…
这篇关于通过Woocommerce中的钩子定制循环产品图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!