Support

Account

Home Forums General Issues How to Randomize This? Reply To: How to Randomize This?

  • 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;
    }