我想为我的一些产品显示不同的缩略图,如果它们出现在特定的类别中。
我已经从特定的类别中删除了当前的缩略图,并且我正在使用一个自定义字段来为我想要替换的产品拉出新的缩略图,这非常有效。然而,当我尝试调用其余产品的普通缩略图时,它不起作用--有什么想法吗?
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();
}
}
发布于 2021-03-28 15:07:21
您需要对woocommerce_get_product_thumbnail
执行echo
。检查下面的代码。
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();
}
}
发布于 2021-04-02 00:10:58
下面的函数需要一个post id,这就是你传递给它的内容吗?
wp_get_attachment_image();
作为if条件的一部分,您可以检查该值是否为正确的值类型:
is_numeric($testCatThumb);
我倾向于创建可读的变量,而不是每次阅读时都要用大脑解析可怕的条件:
function add_test_category_thumbnails() {
global $post;
$testCatThumb = get_post_meta( $post->ID, 'step_dad_mug', true);
$isPostId = !empty($testCatThumb) && is_numeric($testCatThumb);
$isValidAttachmentId = is_product_category('test-category') && $isPostId;
$image = $isValidAttachmentId ? wp_get_attachment_image($testCatThumb) : '';
// display custom image, or fallback to woocommerce thumbnail
echo !empty($image) ? $image : woocommerce_get_product_thumbnail();
}
https://stackoverflow.com/questions/66838100
复制相似问题