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)”.
3. Filter the gallery attributes
Add the following code at the end of functions.php or in your functionality plugin:
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.
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. 🙂