Random background image along with child theme
-
Hello,
I have tried searching here and on the net, despite many discussions, nothing was useful to solve my situation.
I use a child theme and I customized the login screen page. Now I would like to create a background with random pictures. This is currently my setup:CHILD_THEME: the directory with functions.php, style.css and the folder CUSTOM_LOGIN.
CHILD_THEME/CUSTOM_LOGIN: it has the custom_login_style.css and the folder BACKGROUNDS, where are stored background pictures to be randomized.
My reference page was this one: website
Based on that link, I added in the NAME_CHILD_THEME/functions.php file this code:
/*** RANDOMIZE BACKGROUND IMAGE ***/ function displayBackground() { $dir = 'custom_login/backgrounds/'; $cnt = 0; $bgArray= array(); /*if we can load the directory*/ if ($handle = opendir($dir)) { /* Loop through the directory here */ while (false !== ($entry = readdir($handle))) { $pathToFile = $dir.$entry; if(is_file($pathToFile)) //if the files exists { //make sure the file is an image...there might be a better way to do this if(getimagesize($pathToFile)!=FALSE) { //add it to the array $bgArray[$cnt]= $pathToFile; $cnt = $cnt+1; } } } //create a random number, then use the image whos key matches the number $myRand = rand(0,($cnt-1)); $val = $bgArray[$myRand]; } closedir($handle); echo('"'.$val.'"'); }
Then, in the CHILD_THEME/custom_login_styles.css I added this code:
background: url(<?php include'../functions.php'; displayBackground();?>);
When I reload the login page, no background picture is being shown. For some reason my code is not correct. Yeah, I know, it may have many mistakes, but
I am not either an expert of PHP nor CSS, so please my apologize for this common and probably already discussed issue.
Thanks for your help.Thanks.
Riccardo
- The topic ‘Random background image along with child theme’ is closed to new replies.