$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>advanced custom fields - Random popup image per user with no repeat|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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

Post a comment

comment list (0)

  1. No comments so far