Skip to main content
deleted 5 characters in body
Source Link
Jnsn_
  • 13
  • 5

I am trying to show random product thumbnails from a specific category. I have so far a randomizing product image butthis code at the problemmoment but it is this showsnot working and will show from all products, how to randomize from a specific product category?

<?php
    $args = array(
    'posts_per_page'   => 1,
    'orderby'          => 'rand',
    'post_type'        => 'product' ); 

     $random_products = get_posts( $args );

     foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
     <?php endforeach; 
     wp_reset_postdata();
?>

I am trying to show random product thumbnails from a specific category. I have so far a randomizing product image but the problem is this shows from all products, how to randomize from a specific product category?

<?php
    $args = array(
    'posts_per_page'   => 1,
    'orderby'          => 'rand',
    'post_type'        => 'product' ); 

     $random_products = get_posts( $args );

     foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
     <?php endforeach; 
     wp_reset_postdata();
?>

I am trying to show random product thumbnails from a specific category. I have this code at the moment but it is not working and will show from all products, how to randomize from a specific product category?

<?php
    $args = array(
    'posts_per_page'   => 1,
    'orderby'          => 'rand',
    'post_type'        => 'product' ); 

     $random_products = get_posts( $args );

     foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
     <?php endforeach; 
     wp_reset_postdata();
?>
Source Link
Jnsn_
  • 13
  • 5

Woocommerce - Show random product thumbnail from specific product category

I am trying to show random product thumbnails from a specific category. I have so far a randomizing product image but the problem is this shows from all products, how to randomize from a specific product category?

<?php
    $args = array(
    'posts_per_page'   => 1,
    'orderby'          => 'rand',
    'post_type'        => 'product' ); 

     $random_products = get_posts( $args );

     foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
     <?php endforeach; 
     wp_reset_postdata();
?>