I have to make a widget from where the user will be able to add a few images and then I will show only some of them, randomly, and it will be automatically changed on page refresh. The problem is that the number of images is not known, it may be 2, it may be 5. I was thinking of using an “add more” jQuery button but I have problems with working on data from an array.
Has somebody done something like this before? If you have another idea about doing this, I’d be glad to hear of it.
Note: If you have knowledge about a widget which uses this kind of function do not hesitate to tell me, maybe I can get inspiration from there.
I have a little to none experience in developing a plugin or widgets, but I believe this is certainly doable in a custom WordPress theme.
If you want this method as a widget. The function on step 2 and 3 will be the same if you use a proper ajax method.
Hope this help.
This can be solved with fairly simple elements: a Shortcode and the Text Widget.
First, we enable the shortcode functionality for the widget:
Useful: Shortcodes everywhere
Then we create a shortcode that will grab images from a given post or page and display a random number of it. As we will load in random order, at each page load the images will vary.
The usage will be
[random-image id="99" total="4"]
. “id” is the post/page ID. You can even leave this post inDraft
state, so it doesn’t show up in the site. “total” is optional and represents the number of images to show, if there are less images attached it doesn’t matter, it’ll show all that exist.You can have a couple of posts for storing the images and use one widget for each. A Custom Post Type could be created and the
get_posts
part complexified upon it, but I’ll leave that as an exercice for the reader 😉Check the comments for further details: