Display 6 per row products in a random order from the latest 20 product posts
-
Hi dear supporters,
i want to Display latest 6 products in a row in a random order from the latest 20 product posts.
My points should be in the algorithm is…..
1. display 6 products in row, 2. from last 20 latest products, 3. in a random order. All these should be in samei want it from the last 20 latest products..
I got following code as a solution from a supporter but it shows very old products as well
function ijab_random_products() { ?> <ul class="products columns-3"> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 20, 'orderby' => 'date', 'no_found_rows' => true, ); $counter = 1; $max = 6; $loop = new WP_Query( $args ); shuffle($loop->posts); if ( $loop->have_posts() ) { while ( ($loop->have_posts()) && ($counter <= $max) ) : $loop->the_post(); wc_get_template_part( 'content', 'product' ); $counter++; endwhile; } else { echo __( 'No products found' ); } wp_reset_postdata(); ?> </ul> <?php } add_shortcode( 'randomprod', 'ijab_random_products' );
Can we do like this, the logic..
First we sort products by the date,
Second take 20 products starting from the first row
Third shuffle that 20 products we got and display 6 in a rowCan we do this and get this done
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Display 6 per row products in a random order from the latest 20 product posts’ is closed to new replies.