mercredi 8 février 2017

Start array at random position, but follow sequence

What I currently have is an array with image links for a slider. The goal is to get a random starting position in the array, but then follow the sequence of the array.

Basic array: 1 2 3 4 5 6 7 8 9 10 Random start: 4 5 6 7 8 9 10 1 2 3

$attachments = get_children(
    array(
       'post_parent' => $attachment_holder['ID'],
        'post_status' => 'inherit',
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'orderby' => 'rand'));
    }

$attachments2 = get_children(
    array(
        'post_parent' => $attachment_holder['ID'],
        'post_status' => 'inherit',
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'orderby' => 'rand'));


    shuffle($attachments2);
    $attachments2 = reset($attachments2); 
    $attachments2 = array($attachments2);

    $attachments = array_merge($attachments2, $attachments);

Above just outputs a random first image, and then starts with regular sequence.

So: 4 1 2 3 4 5 6 7 8 9 10

I have the feeling array_chunk or array_slice should help me out, but I'm not too sure.




Aucun commentaire:

Enregistrer un commentaire