0
votes

I’m new to Advanced Custom Fields and a novice in WP, I'm interested in creating a random slider in my homepage where each post has many images.

I’m not 100% sure how to combine the wp_query with the ACF repeater, where multiple posts are involved, I did succeed in doing this in a single post page.

I am less interested in specs on how to do this, nor PHP functions, i’m well versed in both, the issue is the WP functions and conventions

If someone already done something like this and can advise how to begin this with combining the ACF repeater functions together with the wp_query, from there i'd know how to shuffle the images of each post with array_rand.

if a Gist/fiddle exists, would be even better.

1
I think all you need is in the ACF Examples. There is a wp-loop and code on repeater fields. But you have probably seeen that. If you show us what you have tried so far, we can also answer more specifically.niklas

1 Answers

0
votes

You could use the shuffle PHP function to randomise the array the repeater field outputs then slice out the amount of slides that you want. Something like this:

$rows = get_field('repeater_field_name'); // Get row array
shuffle($rows); // Shuffle the array in a random order
$rows = array_slice($rows, 0, 5); // Slice out the first 5 elements of the array

if($rows)
{
    echo '<ul>';

    foreach($rows as $row)
    {
        echo '<li>sub_field_1 = ' . $row['sub_field_1'] . ', sub_field_2 = ' . $row['sub_field_2'] .', etc</li>';
    }

    echo '</ul>';
}

If you're just using the repeaters for images, I would suggest using the "Gallery" element rather than the repeater, and doing the same thing.