function product_custom_loop($atts){ ob_start(); $atts = shortcode_atts(array( 'posts_per_page' => 8, 'category' => '', ), $atts); $args = array( 'post_type' => 'product', 'posts_per_page' => $atts['posts_per_page'], ); if (!empty($atts['category'])) { $args['tax_query'] = array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $atts['category'], ), ); } $results = new WP_Query($args); // echo '<pre>'; // var_dump($atts['category']); ?> <div class="row product_row <?php echo (! empty($atts['category'])) ? 'list_product': ''; ?>"> <?php if($results->have_posts()): while($results->have_posts()): $results->the_post(); global $product; $categories = get_the_terms(get_the_ID(), 'product_cat'); ?> <div class="<?php echo (! empty($atts['category'])) ? 'col-md-12': 'col-md-3'; ?>"> <div class="product_wrap"> <div class="product_img_wrap"> <a href="<?php echo esc_url(get_permalink()); ?>"> <?php the_post_thumbnail('full'); ?> </a> </div> <div class="product_content"> <?php if(!$atts['category']){ if ($categories && !is_wp_error($categories)) { $category_names = array(); foreach ($categories as $category) { $category_names[] = $category->name; } echo '<div class="pro_cat">' . implode(', ', $category_names) . '</div>'; } } ?> <a class="proTitle" href="<?php echo esc_url(get_permalink()); ?>"><?php echo esc_html(get_the_title()); ?></a> <div class="product_price-pack"> <div class="product_price"> <?php global $product; if ($product->is_type( 'simple' )) { ?> <?php echo $product->get_price_html(); ?> <?php } ?> <?php if($product->get_type()=='variable') { $available_variations = $product->get_available_variations(); $variation_id=$available_variations[0]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation. $variable_product1= new WC_Product_Variation( $variation_id ); $regular_price = $variable_product1 ->regular_price; $sales_price = $variable_product1 -> sale_price; if (empty($sales_price)) { $sales_price = 0; } } ?> </div> <?php if(!$atts['category']){?> <div class="product_btn"> <a href="<?php echo esc_url( get_site_url() . '/cart/?add-to-cart=' . $product->get_id() ); ?>"> <span class="list-icon fas fa-plus"></span> </a> </div> <?php } ?> </div> </div> </div> </div> <?php endwhile; endif; ?> </div> <?php wp_reset_postdata(); return ob_get_clean(); } add_shortcode('wp-product','product_custom_loop'); [wp-product posts_per_page="4" category="best-selling"]