I am trying to make my wordpress site load a different background image on the homepage every time I refresh the page.
The site is http://americasfinestlighting.com
I would like to add a function to my functions.php file or add a script to my home.php file.
I am curently loading the home page background image with the following css
body.home {
background-image: url("images/home-bg/website-background1.jpg");
background-repeat: no-repeat;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='.myBackground.jpg', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='myBackground.jpg', sizingMethod='scale')";
height: 100%;
width: 100%;
}
The other images I would like the site to load randomly are in the images/home-bg directory.
Thanks,
William
Add the code above to your wordpress themes functions.php file.
It will add a random body class from
background_1
tobackground_10
now you can add css for them as you want.
Just like this. Hope this solves your problem.
You will have to get all the files in the directory like so make sure to exclude . and .. they will be returned by readdir.
Then get a random key like this from the files array (There is a possibility you get the same in a row)
Then with your key you can get the value from the the files array.
When you put it all together you get this
Now where you want to set the actual background image that is up to you. You will have to place
echo 'background-image: url("images/home-bg/'.getRandomImage().'")';
some where.