11

Get Data in Random Order From an ACF Repeater Field

 3 years ago
source link: https://pineco.de/snippets/get-data-in-random-order-from-an-acf-repeater-field/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Get Data in Random Order From an ACF Repeater Field

Advanced Custom Field is one of my favorite WordPress developer plugins. The repeater field is a feature that is genuinely missing from the WordPress core.

Using the repeater field, we can create any combination (row) of a set of subfields again and again.

Using the field, we can retrieve the saved data through a simple loop. Although one thing is not that easy, to get the rows in a randomized order. To achieve this, we have to use the shuffle() PHP function:

if (have_rows('partner', 'option')) {
    $partners = get_field('partner', 'option');
    shuffle($partners); // Randomize

    foreach($partners as $partner) {
        echo $partner['name'];
        echo $partner['url'];
    }
}

Here, instead of the classic while, we use a foreach to loop the array, and we don’t use the the_row() function, so we have to access the values through an index name.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK