2015-06-30 36 views
0

在WooCommerce循環內顯示瓦片正下方的產品類別可以嗎?Woocommerce - 在迴路中顯示類別

這裏有一個代碼,我已經包含在mytheme/woocommerce/content-product.php,取自Wordpress documentation on get_the_category();但它似乎沒有輸出任何東西

 <a href="<?php the_permalink(); ?>" class="titulo-insumo" title="<?php the_title(); ?>"> 
      <?php the_title(); ?> 
     </a> 
     <div class="label-group"><!--Categories *should* be outputed here --> 
     <?php 
     $categories = get_the_category(); 
     $separator = ' '; 
     $output = ''; 
     if($categories){ 
       foreach($categories as $category) { 
         $output .= '<a href="'.get_category_link($category->term_id).'" class="label bg-terciary" title="' . esc_attr(sprintf(__("Ver todos los artículos en la categoría %s"), $category->name)) . '">'.$category->cat_name.'</a>'.$separator; 
       } 
     echo trim($output, $separator); 
     } 

     ?> 

     </div> 

回答

0

嗯,它沒有花費太多的解決,所以我在這裏發佈我的結果。 類別使用the get_the_terms() wordpress function outputed和鏈接outputed用一個簡單的GET請求(/?product_cat=),至極我認爲不會對任何漂亮的永久鏈接選項的工作原理啓用

<?php 
     $terms = get_the_terms($post->ID, 'product_cat'); 

     if ($terms && ! is_wp_error($terms)) : //only displayed if the product has at least one category 

       $cat_links = array(); 

       foreach ($terms as $term) { 
         $cat_links[] = '<a class="label bg-terciary" href="'.get_site_url().'/?product_cat='.$term->slug.'" title="'.$term->name.'">'.$term->name.'</a>'; 
       } 

       $on_cat = join(" ", $cat_links); 
     ?> 

     <div class="label-group"> 
      <?php echo $on_cat; ?> 
     </div> 

     <?php endif; ?> 
相關問題