【发布时间】:2021-06-24 12:45:18
【问题描述】:
如果我的某些产品出现在特定类别中,我想为它们显示不同的缩略图。
我已从特定类别中删除了当前缩略图,并且我正在使用自定义字段来为我想要替换的产品提取新缩略图,这非常有效。但是,当我尝试调用其余产品的正常缩略图时,它不起作用 - 有什么想法吗?
add_action( 'woocommerce_before_main_content', 'remove_test_category_thumbnails', 10 );
function remove_test_category_thumbnails() {
if (is_product_category('test-category')) {
remove_action('woocommerce_before_shop_loop_item_title' , 'woocommerce_template_loop_product_thumbnail' , 10);
}
}
add_action( 'woocommerce_before_shop_loop_item_title', 'add_test_category_thumbnails', 10 );
function add_test_category_thumbnails() {
global $post;
$testCatThumb = get_post_meta( $post->ID, 'step_dad_mug', true);
if (is_product_category('test-category') && (isset($testCatThumb)) ) {
echo wp_get_attachment_image( $testCatThumb );
}
else
echo woocommerce_get_product_thumbnail();
}
}
【问题讨论】:
-
仅
isset()检查是不够的。如果$testCatThumb变量返回一个空值,isset($testCatThumb)将返回 true,因为它只检查变量是否已声明且不为 NULL。在您的情况下,您还应该添加! empty ($testCatThumb)。所以 if 语句可以是:if ( is_product_category('test-category') && ( isset($testCatThumb) && ! empty($testCatThumb) ) ) {. -
感谢您的回复。我已经尝试过您的代码,它似乎显示原始缩略图,但不是我要替换的那个。有什么想法吗?
-
有两种选择:您不在带有
test-categoryslug 的产品类别页面中,或者该产品没有step_dad_mug自定义帖子元。
标签: php wordpress woocommerce hook