Random popup image per user with no repeat

I am pulling images from an ACF gallery that resides on another page. What I intend to do with this is to have these images displaying full screen on my home page on a popup every time the page loads. However, when a website user lands on the home page a random image should popup but only once, so if he navigates to other pages and then back to the home page again these images should not popup again and when the user revisits the website again he should get a different image from his previous visit. This is the code I have so far:

$masonry_photos_year = get_field('masonry_gallery_of_the_year', 600/*1168*/); ?>
<?php if( $masonry_photos_year ) :

   $masonry_photos_year = array_unique($masonry_photos_year);
   $masonry_photos_year = array_rand($masonry_photos_year);

 ?>
<div id="full-image" class="white-popup mfp-with-anim mfp-hide">
    <?php

    foreach ( $masonry_photos_year as $masonry_photo_year ) :
    ?>
    <div class="full-image-info-wrapper">
      <div class="container-fluid">
        <div class="col-sm-6">
          <?php if( $masonry_photo_year['description'] ) : ?>
            <div class="photo-year">
              <h3><?php echo esc_html( $masonry_photo_year['description']); ?></h3>
            </div>
         <?php endif; ?>
        </div>
        <div class="col-sm-6">
          <?php if( $masonry_photo_year['caption'] ) : ?>
            <div class="photo-name">
              <h3><?php echo esc_html( $masonry_photo_year['caption']); ?></h3>
            </div>
          <?php endif; ?>
        </div>
      </div>
    </div>
        <img src="https://wordpress.stackexchange.com/questions/268530/<?php echo $masonry_photo_year["sizes']['post-xlarge'] ?>" class="img-responsive" alt="https://wordpress.stackexchange.com/questions/268530/<?php echo $masonry_photo_year["alt']; ?>">
  <?php endforeach; ?>
  </div>
<?php endif; ?>

Images are displaying on the popup as expected but I am stuck on how to implement the random bit and to ensure that the same image does not display in succession on a next user visit. How could I achieve this?

1 Answer
1

move $masonry_photos_year = array_rand($masonry_photos_year); inside foreach

Leave a Comment