• Skip to primary navigation
  • Skip to main content
OsomCode

OsomCode

Awesome code tutorials for WordPress and Genesis

  • Subscribe
  • My account
    • Edit profile
    • Favorites
    • Update credit card
  • Login

Display WordPress gallery images in random order

Discover in this tutorial how you can display WordPress gallery images randomly with a little bit of code.

The gallery block is very powerful and allows you to customize quite a few parameters, but the option to display images randomly is not one of them.

Luckily, there is a filter that allows you to achieve this easily. Let’s see how.

Steps to display WordPress gallery images in a random way

1. Create the gallery

Create the gallery with the images you want, just as you usually do.

2. Add a custom CSS class

Once you have created the gallery, go to the “Advanced” section and add random in the block “Additional CSS class(es)”.

add CSS classes editor WordPress blocks

3. Filter the gallery attributes

Add the following code at the end of functions.php or in your functionality plugin:

To see this and another 1097 code snippets of this website, login or subscribe here.

In this code you use the render_block_data filter to identify the gallery blocks(core/gallery) that have the random class to display the images randomly.

Conclusion

Now you know how to display WordPress gallery images randomly with a little bit of code.

If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.

Benefits of being a subscriber. 🙂

Block Editor PHP WordPress

To leave read and make questions about this code, you can login or register.

  • About OsomCode
  • FAQ
  • Contact

Legal Notice · Privacy Policy · Cookie Policy · Terms and conditions
Copyright © 2026

close-icon

Lost your password?