2015-06-30 35 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; ?> 
相关问题