Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to retrieve the category name of a WooCommerce product displayed in a WordPress loop and use it as the class for an li (also inside the loop). I've tried this:

 <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>

This is the part where I'm trying to retrieve:

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

but it just outputs this:

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

which not only doesn't retrieve the category but also screws with the markup, breaking the loop.

I've also tried this:

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

but because WooCommerce uses taxonomies, it retrieves the tags but not the product category.

This question is a follow up on this.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

I'm guessing you haven't set or called the $product object, so it can't access any information in it.

Try inserting global $product just before your while loop.

If that doesn't work, try doing a print_r($product->get_categories() outside of your html for debugging.

Without the rest of your code I cannot suggest anything else. Also, you should either make sure not more than one category per product is assigned, or take care of that in your code too.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.