如何显示所有产品类别图像

时间:2019-05-31 10:40:58

标签: wordpress

我在wordpress中获取所有产品类别的图像时遇到问题。请帮助我解决此错误。

这是我的代码-

          <div class="row">
          <?php $sub_count=1; foreach ($product_categories as $key => $category_) { 
         $cat_thumb_id = get_woocommerce_term_meta( $category_->term_id, 'thumbnail_id', true );
        $shop_catalog_img = wp_get_attachment_image_src( $cat_thumb_id, 'shop_catalog' );
            if($sub_count<5){ ?>
           <div class="col-md-6 col-sm-6 col-xs-12 nogap">
            <div class="bannerbox2img">
           <a href="<?php echo get_term_link($category_); ?>"><img src="<?php echo $shop_catalog_img; ?>" alt="<?php echo $category_->name; ?>" /></a>
          <?php echo '<a href="'.get_term_link($category_).'" >';
                echo $category_->name;
                echo '</a>'; ?>
          </div> 
           </div>
          <?php } $sub_count++; } wp_reset_query(); ?>
          </div> 

预先感谢

1 个答案:

答案 0 :(得分:0)

    Please try this code    

    <div class="row">   
<?php
// Get all product categories
$product_category_terms = get_terms( array(
    'taxonomy'   => "product_cat",
    'hide_empty' => 1,
));

$sub_count=1;
foreach($product_category_terms as $term){
    // Get the term link (if needed)
    $term_link = get_term_link( $term, 'product_cat' );

    ## -- Output Example -- ##

    if($sub_count<5){
        echo '<div class="col-md-6 col-sm-6 col-xs-12 nogap">';
            echo '<div class="bannerbox2img">';

            // The link (start)
            echo '<a href="' . $term_link . '" style="display:inline-block; text-align:center; margin-bottom: 14px;">';

            // Display the product category thumbnail
            woocommerce_subcategory_thumbnail( $term );

            // Display the term name
            echo '<a href="' . $term_link . '" >';
                echo $term->name;
            echo '</a>';

            // Link close
            echo '</a>';

            echo '</div>';
        echo '</div>';
    }
     $sub_count++;
} wp_reset_query(); ?>
</div>