Home > Software engineering >  Adding css class to featured products in woocommerce
Adding css class to featured products in woocommerce

Time:04-26

I would like to loop throught all products in shop/category page and add special css class to product if it is featured. I have tried to do this with add_filter hook but it seems I am to dumb to get it working.

CodePudding user response:

You were right about using a filter. I don't know which filter you were trying but you would edit the post_class and add a class if the post type is a product and has the specific category you want.

add_filter( 'post_class', 'namespace_featured_posts' );
function namespace_featured_posts( $classes ) {
    if( 'product' == get_post_type() && has_term( 'featured', 'product_cat') ) {
        $classes[] = 'featured-post';
        return $classes;
    }
}
  • Related