简体   繁体   中英

ACF Repeater random order all fields

Looking to output a Advanced Custom Fields repeater in random order. I have a repeater field "profiles" containing 5 "profile" post objects.

My code today is:

// Randomize and shuffle the rows
$rows = get_sub_field('profiles');
shuffle($rows);
$rand_repeater_fields = array_rand( $rows , 4 ); ?>

<?php if( have_rows('profiles') ):
$stage_index = 0; ?>
<?php while ( have_rows('profiles') ) : the_row();
    // print rows only if in array
    if (in_array(get_row_index() - 1, $rand_repeater_fields)) { ?>

        <?php $post_object = get_sub_field('profile'); //row w. post object start
            if( $post_object ): 
            $post = $post_object;
            setup_postdata( $post ); ?>
                <?php the_permalink();?>
            <?php wp_reset_postdata(); ?>
        <?php endif; ?> //row w. post object end

    <?php // increment index
        $stage_index++;
    } ?>
<?php endwhile; ?>

This code successfully output 4 of 5 rows in random order, which means that it almost works as I wish.

How do I update this snippet to output ALL fields of the repeater fields, in random order, even if they are 3 or 10 in total?

Thankful for any suggestions!

<?php
$args = array(
    'post_type' => 'page',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'rand',
    'meta_query' => array(
        array(
            'key' => 'profiles',
            'compare' => 'EXISTS'
        )
    )
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        $profiles = get_field('profiles');
        if( $profiles ):
            foreach( $profiles as $profile ):
                echo '<div class="profile">';
                    echo '<h2>' . $profile['profile'] . '</h2>';
                echo '</div>';
            endforeach;
        endif;
    }
}
wp_reset_postdata();
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM