I was recently working on a project where we wanted to display random images from pre-defined galleries. I created a custom widget that did just this.
The items already setup before creating this widget were:
- Created custom post type ‘gallery’. Each post held a gallery of images.
- Used ACF (Advanced Custom Fields) to create a nice gallery content area on each ‘Gallery’ post
- Setup PrettyPhoto to open each gallery’s photos in a nice modal
Armed with a couple custom galleries created, the next task was to figure out how to choose some random images from these galleries for display in the sidebar. So I put together this code for the widget output. I’ll go ahead and break down each section:
1. Setup the loop to get all the gallery IDs
2. Next, lets assign a random post to a new variable called $other_page
3. Now we’re ready to plug that into our ACF loop.
What I don’t go into detail here are all the options available when working with images on ACF. For that, I refer you to the documentation available from the ACF site.
4. Lastly, lets put a button to view more images. The URL for this button pulls from another field I created in the widget.
So hopefully that got you a good idea of how you can grab a random gallery from ACF galleries. If I didn’t go into enough detail, let me know and I’ll expound on it.