I am using WP-Ecommerce and would like to show the product’s category on the single product page. I have searched around and found something (posted by Rohan on this site) that almost works:
function cdl_get_cat() {
global $wp_query, $wpsc_query;
$query_data = Array();
$cdl_post_id = wpsc_the_product_id();
$categories = wp_get_object_terms( $cdl_post_id , 'wpsc_product_category' );
//if product is associated w more than one category
if(count($categories) > 1 && isset($wpsc_query->query_vars['wpsc_product_category']))
$query_data['category'] = $wpsc_query->query_vars['wpsc_product_category'];
elseif(count($categories) > 0)
$query_data['category'] = $categories[0]->slug;
return $query_data['category'];
}
echo cdl_get_cat();
While this code does what I need it to, it displays the category name as a slug (i.e. eye-shadow instead of Eye Shadow). I’m not very fluent with PHP so I’m not sure what needs to be changed in order to display the name instead of the slug.
I’ve been struggling with this all day and I’m not great at php either… But I’ve got it to work by deleting this bit (which i didnt understand..):
then changing ‘slug’ to ‘name’ here:
so all you need is this:
if you have more that one category for one product changing the [0] will give you a different one as I think $categories is an array with all associated categories in it. You should probably be able to loop through all of them if needed. I should look up how to do that!
Thanks, found this useful for adding a class to a product of the category it’s in:
Before the product loop:
Then in the loop:
Wow, i think I’ve got it, or got somewhere better…
This should get all the categories associated with the displayed product then loop out a list, separated by commas.