2013-04-16 47 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