Skip to content
Advertisement

Adding css class to featured products in woocommerce

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.

Advertisement

Answer

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;
    }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement