【发布时间】:2020-10-15 22:34:08
【问题描述】:
所以,这是我拥有的 woocommerce 设置。
我有几个类别,我们称它们为 A、B、C、D、E……等等。
从后端,我为每个类别提供类别图像。
现在,假设我在没有选择任何图片的情况下发布了 A 类产品。
该产品仅显示 woocommerce 默认图像(具有灰色背景的图像)。
没有选择图片时,有没有办法使用分类图片作为默认产品图片?
我尝试了一些代码,这是我的最后一个:
add_action( 'init', 'custom_fix_thumbnail');
function custom_fix_thumbnail(){
add_filter('wc_placeholder_img_src', 'custom_woocommerce_placeholder_img_src');
function custom_woocommerce_placeholder_img_src( $src ) {
if (is_shop() || is_singular('product') || is_archive() || is_checkout() || is_cart()) {
global $post;
$array = get_the_terms($post->ID, 'product_cat');
reset($array);
$first_key = key($array);
$thumbnail_id = get_woocommerce_term_meta($first_key, 'thumbnail_id', true);
// get the image URL for parent category
$image = wp_get_attachment_url($thumbnail_id);
// print the IMG HTML for parent category
if ($image)
$src = $image;
}
return $src;
}
}
【问题讨论】:
标签: php wordpress woocommerce