In the comments section of Setting a random full-sized background image in WordPress using Backstretch tutorial, a user asked:
Is there a way to make it choose from images in a gallery so that the client more easily add images without having to get at the files?
In this tutorial I show how a single random image from images uploaded to a Soliloquy slider can be shown as full size background image in the front-end using Backstretch jQuery script. When any page in the front-end is visited or reloaded, one of the slider images will be picked randomly and shown as its background.
Summary of steps:
- Create a "Background Images" Soliloquy slider and upload/select the desired images from which one should be used randomly.
- In a function hooked to
soliloquy_output
filter, loop through all the images in the above slider one by one, store them in an array and return the URL of a random image. - Enqueue Backstretch and initialize it using theĀ above URLĀ sent from PHP to the .js file using
wp_localize_script
.
Sample screenshots:
when the same page is reloaded:
Screencast:
Step 1
Install and activate Soliloquy.
Step 2
Create a slider named say, "Background Images" and upload/select the desired background images. Since we are going to just use it as an easy way to manage the background images, settings in Config and Misc tabs do not matter and need not be configured.
Step 3
Add the following in child theme's functions.php:
To view the full content, please sign up for the membership.
Already a member? Log in below or here.
Hey Sridhar,
I’ve tried implementing this, so that the client can easily manage background pictures rather than them needing to be coded in. However having added this, nothing is showing up in the background. Do I need to change the id in the functions file?
Yes.
You need to replace all instances of 2145 with the ID of your slider.
Hey Sridhar,
I’m still stuck with this. I started again, but now when I add the code to my functions file the whole site stops working and throws up a 500 error.
If you would like me to take a look inside and troubleshoot, send me your WP and FTP logins via https://sridharkatakam.com/contact/.
Hello Sridhar,
I am trying to add this to the agentpress theme and nothing seems to be updating. I noticed a few lines are commented out in the tutorial above regarding the slider id. Line #s 13, 14, 50, 51, and 63 so I removed the comments and changed the 2145 id, but still no luck. I also changed the .js file names on lines 59 and 61 since the agentpress theme already had the same backstretch files but named differently. Any ideas on what I might be missing? Appreciate any help.
[…] In the past I showed how a single random image can be pulled from images uploaded to a Soliloquy slider and set as body background using Backstretch here. […]