首页 > 解决方案 > 获取当前子类别产品拇指 Woocommerce

问题描述

我试图在我的产品页面上获取特定父类别的子类别的产品类别拇指 - 我能够检索给定父类别的子类别的所有图像 - 但我只想显示属于子类别的图像的产品。现在它显示了所有父母孩子的产品上的所有图像。

如何设置它以便它只显示一个子类别,但仍然只显示给定的父类?

代码:

<?php
$catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'child_of'=>'74'));
foreach($catTerms as $catTerm) : ?>
<?php $thumbnail_id = get_woocommerce_term_meta( $catTerm->term_id, 'thumbnail_id', true ); 

// get the image URL
$image = wp_get_attachment_url( $thumbnail_id );  ?>
<li><img src="<?php echo $image; ?>" width="152" height="245"/><span><?php echo $catTerm->name; ?></span></li>
<?php endforeach; ?>

编辑:通过这样做解决:

$terms = get_the_terms( $post->ID, 'product_cat' );
    foreach ( $terms as $term ){
        if ( $term->parent == '174081' )  {
        $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.'">';
         }
    }

标签: phpwordpresswoocommerce

解决方案


推荐阅读