随机化WordPress中的全屏背景图像

时间:2013-10-30 16:05:35

标签: php jquery html css wordpress

我还没有看到这个问题,如果是的话,有人可以重新指导我吗?

我在使用图像库中的随机图像创建WordPress主题的全屏背景时遇到问题。我想编写一个可以在多个站点上使用的PHP函数,所以我不能简单地在代码中使用直接路径。它还需要以这样一种方式在MultiSite上工作,即它只会提取上传到该站点的图像。这是我正在使用的代码:

我背景分区的HTML

<div id="background" class="background" style="background-image:url(<?php displayBackground();?>);">
</div>

PHP随机化我的图片文件夹

<? php
function displayBackground()
{
$uploads = wp_upload_dir();
$img_dir = ( $uploads['baseurl'] . $uploads['subdir'] );
$cnt = 0;
$bgArray= array();

        /*if we can load the directory*/
if ($handle = opendir($img_dir)) {

    /* Loop through the directory here */
    while (false !== ($entry = readdir($handle))) {

    $pathToFile = $img_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.'"');

}

我知道我的CSS标记是正确的,因为如果我给DIV一个固定的图像位置,我会得到一个全屏图像。谁能告诉我该怎么做才能解决它?

0 个答案:

没有答案