WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I figured out how to assign a template to a product category (from this thread Different template of products for specific category. WooCommerce), but I'm wondering if I can assign a custom template to specific products.

EDIT: For more clarity, I want to have a certain style applied to the category page, but when I click on one of those products, I want to keep that same style.

share|improve this question
so basically you want all products of a certain category to have a differente style? – dbeja Dec 19 '12 at 19:55
@dbeja That is correct, I want products of category X to have a custom single-product.php template – miles Dec 19 '12 at 20:22
up vote 2 down vote accepted

If the changes you need are strictly CSS, you can add the category names as classes to the body_class via the body_class filter.

function wpa76627_class_names( $classes ) {
    if( is_singular( 'product' ) ):
        global $post;
        foreach( get_the_terms( $post->ID, 'product_cat' ) as $cat )
            // maybe you want to make this more unique, like:
            // $classes[] = 'product-category-' . $cat->slug;
            $classes[] = $cat->slug;
    return $classes;

This code checks if we are viewing a single product page, then loops over all product_cats, adding the slugs as classes to the body tag. This could also be adapted for any post type or taxonomy.

share|improve this answer
Would I add this to my theme functions file? – miles Dec 19 '12 at 20:24
And I'm not quite there yet. Do I change wpa76627_class_names or slug to the category name? – miles Dec 19 '12 at 20:30
yes, you can just copy/paste that into your functions.php file. you don't have to change anything if you don't want to. in your style.css file, you can target items via a selector like body.some-category-slug h1.product-title to style it per-category – Milo Dec 19 '12 at 20:37
I see what you mean now. I added it to the functions file but when I view source the body tag does not have any classes attached to it. – miles Dec 19 '12 at 20:40
your theme has to have the body_class function: <body <?php body_class(); ?>> – Milo Dec 19 '12 at 20:51

Your Answer


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.