【问题标题】:Display only subcategory product thumbnail woocommerce仅显示子类别产品缩略图 woocommerce
【发布时间】:2019-10-03 10:29:36
【问题描述】:

我在显示子类别的缩略图时遇到了一些问题。谁能帮帮我?

<?php
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
    $category_name = $term->name;
    $category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
    $image = wp_get_attachment_url($category_thumbnail);
    echo '<img src="'.$image.'">';
}?>

我只想显示 SUBCATEGORY 的缩略图,上面的代码对这个问题没有帮助。谢谢

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    请检查以下代码并尝试一下。

    $terms = get_the_terms( $post->ID, 'product_cat' );
    foreach ( $terms as $term ){
        if( $term->parent != 0 ) {
            $category_name = $term->name;
            $category_thumbnail = get_term_meta($term->term_id, 'thumbnail_id', true);
            $image = wp_get_attachment_url($category_thumbnail);
            echo '<img src="'.$image.'">';
        }
    }
    

    检查 Parent id 是否不为 0,或者您可以检查 $term->parent > 0。获取 term_id 的术语元。

    get_woocommerce_term_meta 函数可能已弃用,您可以使用 get_term_meta 获取术语元。

    【讨论】:

    • 很高兴它对您有用!请将答案标记为已接受,以便其他人从答案中获得帮助。
    猜你喜欢
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 2021-06-24
    • 2019-01-07
    • 1970-01-01
    • 2014-04-06
    相关资源
    最近更新 更多