• Resolved gevcen

    (@gevcen)


    Dear,

    I want to assign a CSS code to following product category. How can I do this?

    Thanks

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • 9dotbox

    (@9dotbox)

    You’ll need to add a function to your theme’s functions.php file:

    // add taxonomy term to body_class
    function woo_custom_taxonomy_in_body_class( $classes ){
    if( is_singular( 'product' ) )
    {
    $custom_terms = get_the_terms(0, 'product_cat');
    if ($custom_terms) {
    foreach ($custom_terms as $custom_term) {
    $classes[] = 'product_cat_' . $custom_term->slug;
    }
    }
    }
    return $classes;
    }
    add_filter( 'body_class', 'woo_custom_taxonomy_in_body_class' );

    This adds the product category to the product page’s body tag.

    • This reply was modified 7 years ago by 9dotbox.
    • This reply was modified 7 years ago by 9dotbox.
    • This reply was modified 7 years ago by 9dotbox.
    Plugin Support John Coy a11n

    (@johndcoy)

    Automattic Happiness Engineer

    Hi @gevcen

    We haven’t heard back from you in a while so we will mark this thread as closed. Please open a new thread should you have further questions.

    Thanks,

    Thread Starter gevcen

    (@gevcen)

    @johndcoy

    Thanks for the code. Once I add it, can you confirm that the CSS class to target is :

    woo_custom_taxonomy_in_body_class

    but how will I then distinguish 1 category from another category?

    Thanks

    • This reply was modified 6 years, 10 months ago by gevcen.
    • This reply was modified 6 years, 10 months ago by gevcen.
    • This reply was modified 6 years, 10 months ago by gevcen.
    jessepearson

    (@jessepearson)

    Automattic Happiness Engineer

    @gevcen If using the code above, the CSS class to target would be product_cat_SLUG, where SLUG is replaced with the categories slug.

    The slug can be found by viewing the categories under Products > Categories, they are lower case with spaces replaced with dashes.


    Link to image: https://cld.wthms.co/obYueF

    Due to the age of this thread I am setting it to resolved. If you still need further assistance you can change the status to not resolved.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘How to define css class to a specific woocommerce category’ is closed to new replies.