2015-06-30 5 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

Ну, это не заняло много времени, чтобы понять, поэтому я отправляю свой результат здесь. Эта категория outputed с помощью the get_the_terms() wordpress function и ссылка outputed простого запроса GET (/?product_cat=), которым я предполагаю, что не буду работать для любого типа опции довольно Permalinks включена

<?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; ?> 
Смежные вопросы