How to Display a Random Image in WordPress

Want to spice up your WordPress site with a bit of randomness? Showing a different image each time a page loads can make your site feel more dynamic and fun. Luckily, it’s easy to do! Let’s explore some simple ways to display a random image on your WordPress site.

Why Use Random Images?

Random images make your site more engaging. They add variety and keep things fresh for visitors. Here are a few great reasons to use them:

  • Make your homepage or banner more interesting.
  • Showcase a gallery of product images dynamically.
  • Add a fun surprise element for visitors.

Method 1: Using a Plugin

If you’re not a fan of coding, a plugin is your friend! There are several great WordPress plugins available to handle random image displays.

Recommended Plugins:

  • Random Images Plugin: Simple and effective.
  • WP Random Image: Offers more customization.
  • Dynamic Images: Great for banners and backgrounds.

To install a plugin:

  1. Go to Plugins > Add New in your WordPress dashboard.
  2. Search for a random image plugin.
  3. Click Install and then Activate.
  4. Follow the plugin instructions to set up your random image.
Fallback On WordPress Logins

Method 2: Using a Simple Code Snippet

Want more control? A little bit of PHP can do the trick. This method is perfect for developers or anyone comfortable adding code to WordPress.

Step 1: Upload Your Images

First, upload a few images to your media library or a custom folder inside your theme.

Step 2: Add This Code to Your Theme

Open your theme’s functions.php file and insert this snippet:


function random_image() {
  $images = array(
    "image1.jpg",
    "image2.jpg",
    "image3.jpg",
  );

  $rand_image = $images[array_rand($images)];
  echo 'Random Image';
}

Step 3: Display It Anywhere

Now, place this function in your theme file (like header.php or sidebar.php):


<?php random_image(); ?>
no code

Method 3: Using Shortcodes

Shortcodes make things super easy! Add this code to functions.php to create a simple random image shortcode:


function random_image_shortcode() {
  $images = array(
    "image1.jpg",
    "image2.jpg",
    "image3.jpg",
  );

  $rand_image = $images[array_rand($images)];
  return 'Random Image';
}
add_shortcode('random_image', 'random_image_shortcode');

Now, just type [random_image] in any post or page to display a random image!

Bonus: Using JavaScript for Even More Control

If you prefer not to use PHP, here’s a simple JavaScript method:


<script>
  var images = ["image1.jpg", "image2.jpg", "image3.jpg"];
  var randIndex = Math.floor(Math.random() * images.length);
  document.write('<img src="' + images[randIndex] + '" alt="Random Image">');
</script>

Paste this inside an HTML widget or inside your theme file.

Conclusion

There are many ways to display a random image in WordPress. You can use a plugin for simplicity, PHP for control, or JavaScript for flexibility. Try different methods and see what works best for you!

Drag and drop data option in WP All Import plugin - expanding the images section

Now, go make your WordPress site more exciting and unpredictable!

Have a Look at These Articles Too

Published on February 7, 2025 by Ethan Martinez; modified on February 18, 2025. Filed under: .

I'm Ethan Martinez, a tech writer focused on cloud computing and SaaS solutions. I provide insights into the latest cloud technologies and services to keep readers informed.