Support

Account

Home Forums General Issues How to Randomize This?

Solving

How to Randomize This?

  • Hey, have this ACF code that pulls the latest 2 entries and am wondering how to adjust this so it just pulls random entries instead. Thanks!

    function getLatestPhotos() {
        if(get_field('photos', 34)) {
            $latestPhotos = '<ul class="photo-list">';
            $paperCounter = 0;
    
            while(has_sub_field('photos', 34) && $paperCounter < 2) {
                $latestPhotos .= '<a href="/photos/">';
                $latestPhotos .= get_image_with_alt('cover', get_the_ID(), '');
                $latestPhotos .= '<p>'.get_sub_field('title').'</p>';
                $latestPhotos .= '<span class="name">'.get_sub_field('name').'</span>';
                $latestPhotos .= '</a>';
    
                $paperCounter++;
            }
    
            $latestPhotos .= '</ul>';
        }
        return $latestPhotos;
    }
  • Took out $paperCounter for now to try and get 1 random one pulled and have it functioning with the exception of the get_image_with_alt line. Any clue how I could pull 2 randoms, and adjust the line get_image_with_alt to be random? Feel like I am getting close but need a bit of help.

    function getLatestPhotos() {
        if(get_field('photos', 34)) {
            $rows = get_field('photos', 34);
            $row_count = count($rows);
            $i = rand(0, $row_count - 1);
    
            echo $rows[$i]['sub_field_name'];
    
            $latestPhotos .= '<a href="/photos/">';
            $latestPhotos .= get_image_with_alt('cover', get_the_ID(), '');
            $latestPhotos .= '<p>'.$rows[$i]['title'].'</p>';
            $latestPhotos .= '<span>'.$rows[$i]['name'].'</span>';
            $latestPhotos .= '</a>';
    
        }
        return $latestPhotos;
    }
  • Ok, I simplified this and got the random part to work but now it is pulling two identical random items vs. 2 different random items. How can I fix this?

    function getLatestBooks() {
        if(get_field('books', 34)) {
            $rows = get_field('books', 34);
            $row_count = count($rows);
            $i = rand(0, $row_count - 1);
    
            echo $rows[$i]['sub_field_name'];
    
            $paperCounter = 0;
    
            while(has_sub_field('books', 34) && $paperCounter < 2) {
                $latestBooks .= '<p>'.$rows[$i]['cover'].'</p>';
                $latestBooks .= '<p>'.$rows[$i]['title'].'</p>';
    
                $paperCounter++;
            }
    
            $latestBooks .= '</ul>';
        }
        return $latestBooks;
    }
  • 
    $rows = get_field('books', 34);
    if (!empty($rows)) {
      $max_to_show = 2;
      $row_count = count($rows);
      $shown_rows = array();
      $i = 0;
      for ($loop=0; $loop<$max_to_show; $loop++) {
        // show 2 images
        if ($row_count > $max_to_show) {
          // only randomize if more than max to show
          $i = rand(0, $row_count - 1);
          while (in_array($i, $shown_rows)) {
            // make sure it's not duplicate
            $i = rand(0, $row_count - 1);
          }
          
        } // end if > max to show
          
        // ********************************
        // output row $i here
        // ********************************
          
        $shown_rows[] = $i;
        
        if ($row_count <= $max_to_show) {
          // increment value in cases rows is <= max to show
          $i++;
          if ($i >= $row_count) {
            // make sure that we don't exceed number of rows that exist
            break;
          }
        }
        
      } // end for
    } // end if rows
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.