0

I have a list of images and I have a wordpress loop.

I use the randompics() function to generate a random image to use as a background to the prreview link. However, Im finding that Im generating repeat images here and there and it doesnt look great.

I would like to find a way to ensure that I dont have the same image more than once. There are only ever 10 iterations of the loop on any one page.

Here is my function.

function randompics() {
  $arrayName = array(
    get_template_directory_uri().'/images/jobs_large/1.jpg',
    get_template_directory_uri().'/images/jobs_large/2.jpg',
    get_template_directory_uri().'/images/jobs_large/3.jpg',
    get_template_directory_uri().'/images/jobs_large/4.jpg',
    get_template_directory_uri().'/images/jobs_large/5.jpg',
    get_template_directory_uri().'/images/jobs_large/6.jpg',
    get_template_directory_uri().'/images/jobs_large/7.jpg',
    get_template_directory_uri().'/images/jobs_large/8.jpg',
    get_template_directory_uri().'/images/jobs_large/9.jpg',
    get_template_directory_uri().'/images/jobs_large/11.jpg',
    get_template_directory_uri().'/images/jobs_large/12.jpg',    
    //list goes up to 25 images
  );
 echo $arrayName[array_rand($arrayName)];
}

Heres my loop

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

  <div class="item-container">
    <?php randompics(); ?>
  </div>

<?php endwhile; else : endif;?>

Thanks in advance.

3
  • 2
    Why not use an other variable to serve as an index that increments in every iteration, an then randompics($index); without using array_rand() I mean? Commented Jan 26, 2018 at 16:16
  • you can use a "temp memory". If this generation is user-based you can use the $_SESSION variable to store the images already displayed, otherwise you can choose a file-based temp memory to store. Commented Jan 26, 2018 at 16:16
  • 1
    Create one array of images, shuffle it, then pick from the array in order… Commented Jan 26, 2018 at 16:26

1 Answer 1

1

I suppose the easiest way is to use static variables:

function randompics() {
  static $arrayName = null;
  if (is_null($arrayNme)) {
    $arrayName = array(
      get_template_directory_uri().'/images/jobs_large/1.jpg',
      get_template_directory_uri().'/images/jobs_large/2.jpg',   
      //list goes up to 25 images
    );
    shuffle($arrayName);
  }
  static $index = 0;
  echo $arrayName[$index++];
}

This does share data between function calls, however, which is generally discouraged. A better solution (at least IMHO) would be to use an ImageRandomiser class and have an object of that class keep track of duplicates.

Use, for instance:

class ImageRandomiser
{
  private $images;
  private $index;

  public function __construct(string ...$imageUris)
  {
    $this->index = 0;
    $this->images = $imageUris;
    shuffle($this->images);
  }

  public static function fromDefaults() : self
  {
    return new static(
      get_template_directory_uri().'/images/jobs_large/1.jpg',
      get_template_directory_uri().'/images/jobs_large/2.jpg',
      // ...
    );
  }

  public function imageUri() : string
  {
    $this->index++;
    if ($this->index >= count($this->images)) {
      $this->index = 0;
    }
    return $this->images[$this->index];
  }
}

The loop:

<?php 
$random = ImageRandomiser::fromDefaults();
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

  <div class="item-container">
    <?php $random->imageUri(); ?>
  </div>

<?php endwhile; else : endif; ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.