2

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="<?php echo $masonry_photo_year['sizes']['post-xlarge'] ?>" class="img-responsive" alt="<?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?

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.