Woocommerce在类别页面上显示子类别缩略图

时间:2014-12-20 12:37:05

标签: php wordpress woocommerce

在类别页面上,同一类别中有不同的产品被分配到子类别。在单个产品下方,我想显示产品所分配的子类别的缩略图。我用这段代码来显示缩略图:

    <?php 
        global $wp_query;
        $cat = $wp_query->get_queried_object();
        $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image = wp_get_attachment_url( $thumbnail_id );
        echo wp_get_attachment_image( $thumbnail_id ); 
    ?>  

结果是显示父类别的缩略图(所有这些产品所在的类别)而不是子类别缩略图。

见这里:http://www.solar-discounter.nl/product-categorie/panelen/

我希望有人能得到答案,谢谢你。

2 个答案:

答案 0 :(得分:3)

解决了!

我用以下内容替换了代码:

<?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.'">';
    }
?>

感谢Filespit @ StackExchange

https://wordpress.stackexchange.com/questions/87950/show-category-images-on-single-product-page-and-product-overview-page

答案 1 :(得分:1)

WooCommerce参考说明在themes / yourtheme / functions.php中执行此操作

    add_action( 'woocommerce_archive_description', 'woocommerce_category_image', 2 );
function woocommerce_category_image() {
    if ( is_product_category() ){
        global $wp_query;
        $cat = $wp_query->get_queried_object();
        $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image = wp_get_attachment_url( $thumbnail_id );
        if ( $image ) {
            echo '<img src="' . $image . '" alt="" />';
        }
    }
}