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'm a newbie at modifying wooCommerce. I hope someone can help me with a problem i'm having while trying to make a minor change to the plugin to fit with a request from my client.

I'm trying to modify WooCommerce to display the category name for each product. I want the category name to be shown above the product image on the main shop page. (the view where many products are shown on the screen at once)

I've searched around, and i can't seem to find how to call the product->category to get the category name for each product being shown.

In the wooCommerce files, i've found content-product.php
(in templates/content-product.php)

and i'd like to inject the category for each product by adding to this action: do_action( 'woocommerce_before_shop_loop_item_title' );

I understand that I can add a function in my theme's functions.php to inject some new code into the ''woocommerce_before_shop_loop_item_title'

but I can't work out how to get the category for each product.

can someone help? or tell me where i might be able to find the answer?

any help will be much appreciated, thank you!

share|improve this question

closed as off-topic by Rarst Jun 11 at 10:12

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers 2

up vote 2 down vote accepted

The product title is added to the woocommerce_single_product_summary hook at priority 5, so you'll want to add to the same hook with a lower (earlier) priority. You'd add this to your theme's functions.php file. I presume when you say "category" you mean the WooCommerce product category, so this should return the first one and print it before the product title:

function wpa89819_wc_single_product(){

    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );

    if ( $product_cats && ! is_wp_error ( $product_cats ) ){

        $single_cat = array_shift( $product_cats ); ?>

        <h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>

<?php }
}
add_action( 'woocommerce_single_product_summary', 'wpa89819_wc_single_product', 2 );
share|improve this answer
    
Thanks so much! your solution worked great and yes, you guessed right that I meant the WooCommerce product category, not the wordpress page category. –  user28546 Mar 18 '13 at 22:48
    
Great! If it worked and solved your question can you select it as the answer? –  helgatheviking Mar 18 '13 at 23:09
    
hi helgatheviking, Thanks again for your help. sorry I'm a newbie and i can't see how to mark this as answered?!? –  user28546 Mar 26 '13 at 19:21
    
There should be a check mark next to all the answers. When you click on it it will turn green to signify that this is the correct answer. Choosing answers keeps your percentage up so people will be more likely to help you in the future. Please read the "how do I ask questions" part of the FAQ –  helgatheviking Mar 27 '13 at 8:52
    
thanks again! i've selected the answer now. –  user28546 Mar 30 '13 at 14:17

you need the get_categories function, which takes the following arguments:

get_categories( $separator, $before, $after )

so in it's simples form (assuming only one category per product), just add <?php echo $product->get_categories(); ?> to your content-product.php (line 53). If you have more categories, you can do it like on the single product page:

<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
share|improve this answer
    
note that if you want to display the title above the thumbnail, you should put it in line 43, but this complicates things as any 'sale' tags etc. could stay in place (instead of moving with the thumbnail). –  Ewout Mar 10 '13 at 12:43

protected by Community Jun 11 at 5:59

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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