Title: Random post Image
Last modified: August 19, 2016

---

# Random post Image

 *  Resolved [pixeljam](https://wordpress.org/support/users/pixeljam/)
 * (@pixeljam)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-post-image/)
 * Hi guys!
    I’m trying to modify the Autofocus template, actually I quite stripped
   it down a bit =) I would like to modify the home page retrieving a random image
   from each post instead of the last one.
 *     ```
       function random_image_url($size=large) {
   
       	global $post;
       	if ( $images = get_children(array(
       		'post_parent' => get_the_ID(),
       		'post_type' => 'attachment',
       		'numberposts' => 1,
       		'orderby' => 'rand()',
       		'post_mime_type' => 'image',)))
       	{
       		foreach( $images as $image ) {
       			$attachmenturl=wp_get_attachment_image_src($image->ID, $size);
       			$attachmenturl=$attachmenturl[0];
       			echo ''.$attachmenturl.'';
       		}
   
       	}  else {
       		echo '' . get_bloginfo ( 'stylesheet_directory' ) . '/img/no-attachment.gif';
       	}
       }
       ```
   
 * that is the stripped down version of post_image_url with a different name (in
   case I mess it up :P).
    I tried to add orderby=rand but to the get_children function,
   but doesn’t work, any idea? Cheers!

Viewing 1 replies (of 1 total)

 *  Thread Starter [pixeljam](https://wordpress.org/support/users/pixeljam/)
 * (@pixeljam)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-post-image/#post-1708285)
 *     ```
       Ok solved, I just missed the option to switch get_children with get_posts!! what a dumb!
       Here is the working code if anybody needs a random image ;)
   
       function random_image_url($size=large) {
   
       	global $post;
       	if ( $images = get_posts(array(
       		'post_parent' => get_the_ID(),
       		'post_type' => 'attachment',
       		'numberposts' => 1,
       		'orderby' => 'rand',
       		'post_mime_type' => 'image',)))
       	{
       		foreach( $images as $image ) {
       			$attachmenturl=wp_get_attachment_image_src($image->ID, $size);
       			$attachmenturl=$attachmenturl[0];
       			echo ''.$attachmenturl.'';
       		}
   
       	}  else {
       		echo '' . get_bloginfo ( 'stylesheet_directory' ) . '/img/no-attachment.gif';
       	}
       }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Random post Image’ is closed to new replies.

## Tags

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

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 1 participant
 * Last reply from: [pixeljam](https://wordpress.org/support/users/pixeljam/)
 * Last activity: [15 years, 8 months ago](https://wordpress.org/support/topic/random-post-image/#post-1708285)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
