Simple wordpress random image function, display random image without plugin, display one image or more than image, and shortcode random image.
WordPress Random Image
Using get_posts() function, we will display random image.
WordPress Random Image Function
Copy function code and paste it in your functions.php file, you can use it inside another file, for example you can display random image in header.php file or in custom widget. You can paste code in functions.php file and use it inside another file, just call function name like this:
<?php wordpress_random_image(); ?>
function wordpress_random_image(){ // Function by Qassim Hassan, https://wp-time.com - Twitter @QQQHZ $args = array( 'post_type' => 'attachment', 'post_status' => 'any', 'orderby' => 'rand', // select image by random. 'posts_per_page' => 1, // number of random image, default is 1 random image, you can change number to for example 10 to display 10 random images. 'post_mime_type' => array('image/png', 'image/x-png', 'image/jpeg', 'image/jpg', 'image/gif', 'image/bmp') // image mime type (image format), you can remove some mime type or adding custom mime type. ); $images = get_posts($args); foreach ($images as $image) { $image_link = wp_get_attachment_url($image->ID); // random image link. $image_title = $image->post_title; // random image title, you can remove it, not important. $image_caption = $image->post_excerpt; // random image caption, you can remove it, not important. echo '<p><img src="'.$image_link.'" title="'.$image_title.'" alt="'.$image_caption.'"></p>'; // display random image } wp_reset_postdata(); }
Default is 1 random image, to display more random images, for example change nubmer “‘posts_per_page’ => 10” now will be display 10 random images. Default image format is jpg, jpeg, png, gif, bmp, you can adding custom format. This is title=”‘.$image_title.'” image title, you can remove it, not important, and alt=”‘.$image_caption.'” this is image caption, not important, you can remove it. You can remove <p></p> html tag.
WordPress Random Image Shortcode
If you want random image shortcode, paste this function in your functions.php file.
function wordpress_random_image_shortcode(){ ob_start(); wordpress_random_image(); return ob_get_clean(); } add_shortcode('wordpress_random_image', 'wordpress_random_image_shortcode');
Now in your post, use this shortcode [wordpress_random_image] to display random image inside your post.
Shortcode Demo
This is random image:
Refresh this page to changing image.
4 Comments
neon emmanuel
wow brilliant, just what i was looking for, please how do i get the title, and the image link it self?
Qassim Hassan
Read my code, look to $image_title.
Christina
Thank you very, very much for this code. The images here are grabbed from the main media gallery. Is it possible to get the images from a defined folder? Kindly asked, what do I (not really familiar with php) have to change to achieve this?
Dario
…yeah: Is it possible to get the images from a defined folder?