WordPress.org

Ready to get started?Download WordPress

Forums

Printing the product's category (3 posts)

  1. liquid8
    Member
    Posted 2 months ago #

    In the theme I am using I have a loop where $product is accessed and I'd need to figure out if this product belongs to a certain category named "freebie". How could I do this or at least print all of this product's category/category ID's inside a loop?

  2. Evan Herman
    Member
    Posted 2 months ago #

    is_category( 'Stinky Cheeses' );

    where 'Stinky Cheeses' is the name of your category.

    if ( is_category( 'Stinky Cheeses' ) ) {
      echo 'this is in stinky cheeses category!';
    }

    or to list all categories inside of a loop:

    <?php 
    
    $categories = wp_get_post_categories( $post->ID );
    print_r($categories);
    ?>
  3. liquid8
    Member
    Posted 2 months ago #

    Thank you but seems that doesn't work in this case because I am trying to do this inside a loop in a file content_product.php

    https://github.com/woothemes/woocommerce/blob/v2.1.0/templates/content-product.php

    If I could somehow get the ID of the product in loop, I believe it would work then. Now I am getting an empty array if I try to do that second option with $post->ID. $product don't seem to have a value $product->ID accesible. Any hints? :)

Reply

You must log in to post.

About this Topic