WooCommerce-自定义缩略图和默认后备图像占位符 [英] WooCommerce - Custom thumbnails and default fall back image placeholder

查看:279
本文介绍了WooCommerce-自定义缩略图和默认后备图像占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在woocommerce_get_product_thumbnail中插入一个包装器,我可以看到我的包装器出现了,但是如果没有图像,它没有后备图像.

I just want to insert a wrapper to the woocommerce_get_product_thumbnail I can see that my wrapper appeared but It doesn't have a fall back image if there's no image.

如何输出默认的woocommerce缩略图?

How do I output the default woocommerce thumbnail?

这是我不完整的代码:

remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);

if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
    function woocommerce_template_loop_product_thumbnail() {
        echo woocommerce_get_product_thumbnail();
    } 
}
if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {   
    function woocommerce_get_product_thumbnail( $size = 'shop_catalog', $placeholder_width = 0, $placeholder_height = 0  ) {
        global $post, $woocommerce;
        $output = '<div class="col-lg-4">';

        if ( has_post_thumbnail() ) {               
            $output .= get_the_post_thumbnail( $post->ID, $size );
        } else {
            how to show the default woocommerce thumb 
        }                       
        $output .= '</div>';
        return $output;
    }
}

推荐答案

我为remove_action& add_action首先在WordPress/WooCommerce初始化时将其触发...

I have add an init hook for remove_action & add_action at the beginning to fire it when WordPress/WooCommerce Initialises…

自woocommerce 2.0版( 见此以来,不推荐使用$placeholder_width = 0, $placeholder_height = 0 ).您不再需要它们了,这可能是您问题的一部分.

As $placeholder_width = 0, $placeholder_height = 0 is deprecated since woocommerce version 2.0 (see this). You don't need them anymore and it could be part of your problem.

注意: pallavi 的答案正确$output .= wc_placeholder_img( $size );在您的else语句中.我已经为此投票给他...

Note: The answer of pallavi is correct for $output .= wc_placeholder_img( $size ); in your else statement. I have already up voted him for this…

所以我对您的代码做了一些更改:

So I have changed a little bit your code:

add_action('init', function(){
    remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
    add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
}

if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
    function woocommerce_template_loop_product_thumbnail() {
        echo woocommerce_get_product_thumbnail();
    } 
}

if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {   
    function woocommerce_get_product_thumbnail( $size = 'shop_catalog' ) {
        global $post, $woocommerce;
        $output = '<div class="col-lg-4">';

        if ( has_post_thumbnail() ) {               
            $output .= get_the_post_thumbnail( $post->ID, $size );
        } else {
             $output .= wc_placeholder_img( $size );
             // Or alternatively setting yours width and height shop_catalog dimensions.
             // $output .= '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" width="300px" height="300px" />';
        }                       
        $output .= '</div>';
        return $output;
    }
}

这篇关于WooCommerce-自定义缩略图和默认后备图像占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆