通过Woocommerce中的钩子定制循环产品图像

Abd*_*nan 4 php wordpress image woocommerce hook-woocommerce

我正在自定义woocommerce主题。我使用钩子动作woocommerce固定在循环产品上。

要在循环中调用/包含缩略图,我们称此钩子

<?php do_action('woocommerce_before_shop_loop_item_title'); ?>
Run Code Online (Sandbox Code Playgroud)

并出现缩略图。我很困惑位置在哪里<img src"" ....?如何编辑该代码?

谢谢

Loi*_*tec 5

挂钩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.
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,您可以看到它使用了woocommerce_get_product_thumbnail()function

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 ) : '';
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这能回答您的问题,并消除您的困惑。


自定义循环产品图片

现在,您可以使用以下方法从钩子中删除此默认功能以添加自己的自定义功能:

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

    return $product ? $product->get_image( $image_size ) : '';
}
Run Code Online (Sandbox Code Playgroud)

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。

所以现在您只需要自定义函数中的代码...