Title: Random images image.php
Last modified: August 21, 2016

---

# Random images image.php

 *  [Famous](https://wordpress.org/support/users/famous/)
 * (@famous)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/random-images-imagephp/)
 * How can I show random images on my image.php page? Some thumbnails of particular
   posts?
 * Don’t want to use a plugin.
 * Using twentyTwelve theme
 * Thank you

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Thread Starter [Famous](https://wordpress.org/support/users/famous/)
 * (@famous)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/random-images-imagephp/#post-3676024)
 * so I found this, but is there a way to specify how many images show, and stop
   duplicate images?
 *     ```
       <?php
       $query = new WP_Query( array( 'post_status' => 'any', 'post_type' => 'attachment' ) );
       $key = array_rand($query->posts, 1);
       echo wp_get_attachment_image($query->posts[$key]->ID, 'thumbnail');
        ?>
       ```
   
 * _[Please post code & markup between backticks or use the code button. Your posted
   code may now have been permanently damaged by the forum’s parser.]_
 * Thanks
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/random-images-imagephp/#post-3676026)
 * Do not edit the Twenty Twelve theme. It is the default WordPress theme and having
   access to an **unedited** version of the theme is vital when dealing with a range
   of site issues. First [create a child theme for your changes](http://codex.wordpress.org/Child_Themes).
 *  Thread Starter [Famous](https://wordpress.org/support/users/famous/)
 * (@famous)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/random-images-imagephp/#post-3676027)
 * way ahead of you on that Esmi. I always use child themes. Got any ideas on how
   to do it?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/random-images-imagephp/#post-3676030)
 * Have you had a look at [http://wordpress.stackexchange.com/questions/11662/get-all-images-in-media-gallery](http://wordpress.stackexchange.com/questions/11662/get-all-images-in-media-gallery)
 *  Thread Starter [Famous](https://wordpress.org/support/users/famous/)
 * (@famous)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/random-images-imagephp/#post-3676038)
 * not sure about that link. Can you tell me what the link is to take me to an image
   url?
 * Thanks
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/random-images-imagephp/#post-3676039)
 * [wp_get_attachment_url()](http://codex.wordpress.org/Function_Reference/wp_get_attachment_url)
   is the WordPress function that will actually output an image url – like `http://
   example.net/wp-content/uploads/filename.jpg`. o:
 *     ```
       $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 );
       }
       ```
   
 * will grab all of the images in the media library & dump their urls into an array
   called $images. You can use that array of image urls in any way that you want.
   If you want a couple of random images, you could use [PHP’s rand function](http://php.net/manual/en/function.rand.php)
   to generate a couple of random numbers between 0 and `ount( $image )` (which 
   is the number of items in the image array.
 * making sure that one of these random images isn’t a replica of the main displayed
   image could be tricky, though.
 *  Thread Starter [Famous](https://wordpress.org/support/users/famous/)
 * (@famous)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/random-images-imagephp/#post-3676042)
 * I worded that incorrect – I need this next_attachment_url to take me to the image
   page. Not a refresh with a new image. I need it to take me to that images url,
   not the jpg url. Any ideas?
 * Thanks
 *  Thread Starter [Famous](https://wordpress.org/support/users/famous/)
 * (@famous)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/random-images-imagephp/#post-3676062)
 * by the way this doesn’t work:
 *     ```
       <?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 );
       }
       ?>
       ```
   
 *  Thread Starter [Famous](https://wordpress.org/support/users/famous/)
 * (@famous)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/random-images-imagephp/#post-3676066)
 * Can someone please point out code that will show a random thumbnail in twentytwelve
   theme with a link to its post page?
 * Thank you
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/random-images-imagephp/#post-3676104)
 * The code esmi provided works, but it is not a turnkey solution. You need to do
   some of the work. The code just stuffs a bunch of attachment IDs into an array.
   The code you develop needs to randomly grab one ID. With the ID, you can get 
   the image source with `wp_get_attachment_image_src()` and the image’s page permalink
   with `get_permalink()`. With all that you can output all the HTML required to
   show a random thumbnail with a link to it’s page.
 * If you wrap this all in a function, you can call the function from your child
   theme’s template where ever you want the thumbnail to appear.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Random images image.php’ is closed to new replies.

## Tags

 * [image](https://wordpress.org/support/topic-tag/image/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 10 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/random-images-imagephp/#post-3676104)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
