advanced custom fields - Random popup image per user with no repeat

admin2025-06-06  5

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?

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?

Share Improve this question edited May 30, 2017 at 9:30 Paulo asked May 30, 2017 at 9:18 PauloPaulo 213 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749164302a316906.html

最新回复(0)