【问题标题】:Woocommerce - display different product thumbnails on category pageWoocommerce - 在类别页面上显示不同的产品缩略图
【发布时间】: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-category slug 的产品类别页面中,或者该产品没有step_dad_mug 自定义帖子元。

标签: php wordpress woocommerce hook


【解决方案1】:

你需要echowoocommerce_get_product_thumbnail。检查下面的代码。

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();
    } 
}

【讨论】:

  • 感谢您的回复。我一定错过了这一点,因为我一直在尝试许多不同的变化来尝试使其工作,但是添加 echo 并不能解决它。
  • 我已经在我的网站上对其进行了测试,它在具有自定义字段的产品上显示新缩略图(正确),但没有自定义字段的产品不显示产品缩略图。
【解决方案2】:

以下函数需要一个帖子 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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多