0

Im trying to set up a page on my site that has all the images from the media library on it shuffled up in a random order.

As of now I have the strings in a really long array and was wondering how I could turn them into images and "shuffle" them or maybe query them in a random order first and then convert them?

Have been stuck on this for awhile so any tips would be good.

Here is a link to the site http://ownc.co.nz/random/

Cheers

            <?php $query_images_args = array('post_type' => 'attachment', 'post_mime_type' => 'image', 'post_status' => 'inherit', 'posts_per_page' => -1, );

            $query_images = new WP_Query($query_images_args);
            $images = array();
            foreach ($query_images->posts as $image) {
                $images[] = wp_get_attachment_url($image -> ID);
            }

            var_dump($images);
            ?>

1 Answer 1

1

To randomize your images you can add 'orderby' argument to WP_Query with the value of 'rand' http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters alternatively you can shuffle the array

http://php.net/manual/en/function.shuffle.php

As far as converting I am not sure what you mean, if you have an array of urls than just do an image tag with the source of the url:

<?php foreach ($images as $img): ?> 
<img src="<?php echo $img?>">
<?endforeach; ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.