• Resolved cardboardmonet

    (@cardboardmonet)


    I’ve searched high and low and haven’t found a solution to my problem. I’m using the West theme, with the live page composer plugin to display my products on one page, in list format. Right now I have the products displaying as such:

    PRODUCT NAME (Add to Cart)
    PRODUCT NAME (Add to Cart)

    I would like to sneak in the categories each product is in, so it looks like this:

    PRODUCT NAME Cat1 Cat2 (Add to Cart)
    PRODUCT NAME Cat2 Cat3 (Add to Cart)

    I can’t, for the life of me, find a PHP tag that will allow me to add in these categories. Is it possible? Has anyone experienced this issue and found a solution?

    Thank you!
    Ashley

    https://www.ads-software.com/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    The PHP function is https://codex.www.ads-software.com/Function_Reference/get_the_term_list. You’ll need a custom shortcode if it needs to be within a page.

    Thread Starter cardboardmonet

    (@cardboardmonet)

    Thank you, Mike. I’m not a php developer by any means. I don’t need a custom shortcode, since this is going in a template file, but I’m not sure which variable to use in the get_the_term_list. Would you be able to point me in the right direction?

    Thank you!!

    Thread Starter cardboardmonet

    (@cardboardmonet)

    Hi Mike,

    I think I figured out how this get_the_term_list works, but it’s pulling blog posts categories, and I need it to pull the category each specific product is listed in. Can you please advise?

    Thank you!!

    Thread Starter cardboardmonet

    (@cardboardmonet)

    I found the code for those who might be looking. Add this to your functions file:

    global $post;
    $terms = get_the_terms( $post->ID, 'product_cat' );
    foreach ($terms as $term) {
        $product_cat_id = $term->term_id;
        break;
    }

    And then this tag wherever you want the categories to appear:

    <?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Code to display each product's category?’ is closed to new replies.