1

好的,我正在尝试检索显示在 wordpress 循环中的 woocommerce 产品的类别名称,并将其用作循环内的 li 类,我已经尝试过:

 <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 使用分类法,它会检索标签而不是产品类别。任何帮助都非常感谢克里斯亲切的问候

4

1 回答 1

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; 
}
于 2013-04-16T16:46:54.127 回答