• Hi. I have a problem with adding new class for specific product w/ woocommerce.

    I want to have diffrent css style for every product page, which is in category “Drukarki/Kopiarki” (slug: druk-kop).

    My code looks like this:

    add_filter( 'body_class', function( $classes ) {
    	if(is_product_category( 'shirts' )){
    		array_push($classes, 'moja-wlasna-klasa');
    	}
        return $classes;
    } );

    But it generates new class only on product category page. How can I do it for product page?

Viewing 1 replies (of 1 total)
  • Hello kingarchee,
    try following code to add class on category pages and single product page of category “shirts”:

    add_filter( 'body_class', function( $classes ) {
     global $post;
     $terms = wp_get_post_terms( $post->ID, 'product_cat' );
     foreach ( $terms as $term ) $categories[] = $term->slug;
    
     if ( in_array( 'shirts', $categories ) ) {
       array_push($classes, 'moja-wlasna-klasa');
     } else if(is_product_category( 'shirts' )){
       array_push($classes, 'moja-wlasna-klasa');
     }
        return $classes;
    } );
Viewing 1 replies (of 1 total)

The topic ‘Extra class for product page’ is closed to new replies.