2013-04-16 49 views
1

確定我試圖以檢索在一個WordPress循環顯示的woocommerce產品的類別名稱,還可以使用它作爲類鋰循環內我已經試過這樣:woocommerce回顧類別名稱爲div類?

<div id="isocontent" class="products"> 
       <ul><?php while (have_posts()) : the_post(); ?> 


         <li class="<?php echo $product->get_categories(); ?> box"> 
          <a href="<?php the_permalink(); ?>"><?php echo the_post_thumbnail(); ?></a> 
          <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p> 
          <a href="<?php the_permalink(); ?>"><span href="<?php the_permalink(); ?> " class="amount price" data-original="<?php echo get_woocommerce_currency(); ?><?php echo $product->get_price(); ?>" data-price="<?php echo $product->get_price(); ?>" title="Original price: <?php echo $product->get_price(); ?>"><?php echo get_woocommerce_currency(); ?><?php echo $product->get_price(); ?></span></a> 
          <a href="<?php the_permalink(); ?>?add-to-cart=<?php echo $post->ID ?>" class="pbutton">Add to Cart</a> 
         </li> 
        <?php endwhile; ?> 
       </ul> 
      </div> 

這是部分我試圖以檢索與類:

<li class="<?php echo $product->get_categories(); ?> box"> 

,但它只是輸出這樣的:

<li class="&lt;a href=" http:="" localhost.no="" fanny="" kategori="" interior-sv="" "="" rel="tag"> 

這確實檢索類別,但人所以用標記打破循環。 我也試過這樣:

<li <?php post_class('box'); ?>

但由於woocommerce使用taxonmys它retrives的標籤,但不是產品類別。 任何幫助大大appriciated 親切的問候 克里斯

回答

4

它並不像做一個單一的電話一樣簡單 - get_categories()設計,以顯示產品類別的HTML表示。產品類別實際上是一種定製分類法,因此您必須使用get_the_terms()來獲取它。

global $post; 
$terms = get_the_terms($post->ID, 'product_cat'); 
foreach ($terms as $term){ 
    $category_id = $term->term_id; 
    $category_name = $term->name; 
    $category_slug = $term->slug; 
    break; 
} 
+0

我可以適應這個以返回/taxonomy-product_cat.php頁面上的子caterogries嗎? – vimes1984

+0

如果這是產品分配給它,它應該給你默認的子類別。如果您想要遍歷,請查看:http://wordpress.stackexchange.com/questions/56784/get-main-parent-categories-for-a-product – doublesharp