Php 在WordPress中随机化全屏背景图像
我还没有看到这个问题,所以如果是,有人可以重新指导我吗 我在为我的WordPress主题创建全屏背景时遇到了一个问题,该主题使用图像库中的随机图像。我想编写一个可以在多个站点上使用的PHP函数,因此我不能简单地在代码中使用直接路径。它还需要在多站点上工作,以便只提取上传到该站点的图像。以下是我正在使用的代码: 我的背景Div的HTMLPhp 在WordPress中随机化全屏背景图像,php,jquery,html,css,wordpress,Php,Jquery,Html,Css,Wordpress,我还没有看到这个问题,所以如果是,有人可以重新指导我吗 我在为我的WordPress主题创建全屏背景时遇到了一个问题,该主题使用图像库中的随机图像。我想编写一个可以在多个站点上使用的PHP函数,因此我不能简单地在代码中使用直接路径。它还需要在多站点上工作,以便只提取上传到该站点的图像。以下是我正在使用的代码: 我的背景Div的HTML <div id="background" class="background" style="background-image:url(<?php d
<div id="background" class="background" style="background-image:url(<?php displayBackground();?>);">
</div>
另外,我这样做是因为我不喜欢WPSupersize.js脚本影响站点样式的方式。看起来您在class=“background
之后缺少了一个“
。不知道这是帖子中的错误还是代码中的错误。这可能是导致脚本问题的原因。请检查代码以确定,这里只是输入错误。编辑为正确,但谢谢你的眼睛。有人看过这个吗?有人能帮忙吗?
<? 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.'"');
}