WooCommerce - 自定义缩略图和默认后备图像占位符
WooCommerce - Custom thumbnails and default fall back image placeholder
我只想向 woocommerce_get_product_thumbnail 插入一个包装纸 我可以看到我的包装纸出现了,但如果没有图像,它就没有后备图像。
如何输出默认的 woocommerce 缩略图?
这是我的不完整代码:
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( '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 {
$output .= wc_placeholder_img( $size );
}
$output .= '</div>';
return $output;
}
}
我在开头为 remove_action
和 add_action
添加了一个 init
钩子,以便在 WordPress/WooCommerce 初始化时触发它...
因为 $placeholder_width = 0, $placeholder_height = 0
自 woocommerce 2.0 版以来已弃用 (see this)。您不再需要它们,这可能是您问题的一部分。
注意: pallavi 的答案对于 $output .= wc_placeholder_img( $size );
在您的 else 语句中是正确的。我已经为此投了他一票……
所以我对你的代码做了一点修改:
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_get_product_thumbnail 插入一个包装纸 我可以看到我的包装纸出现了,但如果没有图像,它就没有后备图像。
如何输出默认的 woocommerce 缩略图?
这是我的不完整代码:
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( '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 {
$output .= wc_placeholder_img( $size );
}
$output .= '</div>';
return $output;
}
}
我在开头为 remove_action
和 add_action
添加了一个 init
钩子,以便在 WordPress/WooCommerce 初始化时触发它...
因为 $placeholder_width = 0, $placeholder_height = 0
自 woocommerce 2.0 版以来已弃用 (see this)。您不再需要它们,这可能是您问题的一部分。
注意: pallavi 的答案对于 $output .= wc_placeholder_img( $size );
在您的 else 语句中是正确的。我已经为此投了他一票……
所以我对你的代码做了一点修改:
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;
}
}