Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
随机背景与php&;css_Php_Css_Wordpress - Fatal编程技术网

随机背景与php&;css

随机背景与php&;css,php,css,wordpress,Php,Css,Wordpress,我正在尝试为每个页面视图加载不同的背景。不仅可以通过刷新来旋转图像。我有一个代码可以工作,但只在刷新时工作。我相信我可以通过使用脚本或get template函数来解决这个问题,但是我已经尝试了很多我在网上找到的脚本和代码片段,但没有任何效果。我真的很想得到一些指导。在过去的几年里,我已经尝试并放弃了几次,我很想找到一个解决方案。提前感谢您的建议 这是我的PHP代码,它与我的旋转图像放在同一个目录中: <?php $folder = '.'; $extList = array(); $

我正在尝试为每个页面视图加载不同的背景。不仅可以通过刷新来旋转图像。我有一个代码可以工作,但只在刷新时工作。我相信我可以通过使用脚本或get template函数来解决这个问题,但是我已经尝试了很多我在网上找到的脚本和代码片段,但没有任何效果。我真的很想得到一些指导。在过去的几年里,我已经尝试并放弃了几次,我很想找到一个解决方案。提前感谢您的建议

这是我的PHP代码,它与我的旋转图像放在同一个目录中:

<?php

$folder = '.';

$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';

$img = null;

if (substr($folder,-1) != '/') {
    $folder = $folder.'/';
}

if (isset($_GET['img'])) {
    $imageInfo = pathinfo($_GET['img']);
    if (
        isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
        file_exists( $folder.$imageInfo['basename'] )
    ) {
        $img = $folder.$imageInfo['basename'];
    }
} else {
    $fileList = array();
    $handle = opendir($folder);
    while ( false !== ( $file = readdir($handle) ) ) {
        $file_info = pathinfo($file);
        if (
            isset( $extList[ strtolower( $file_info['extension'] ) ] )
        ) {
            $fileList[] = $file;
        }
    }
    closedir($handle);

    if (count($fileList) > 0) {
        $imageNumber = time() % count($fileList);
        $img = $folder.$fileList[$imageNumber];
    }
}

if ($img!=null) {
    $imageInfo = pathinfo($img);
    $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
    header ($contentType);
    readfile($img);
} else {
    if ( function_exists('imagecreate') ) {
        header ("Content-type: image/png");
        $im = @imagecreate (100, 100)
            or die ("Cannot initialize new GD image stream");
        $background_color = imagecolorallocate ($im, 255, 255, 255);
        $text_color = imagecolorallocate ($im, 0,0,0);
        imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
        imagepng ($im);
        imagedestroy($im);
    }
}

?>

您的浏览器将缓存结果。因此,它只会刷新硬重新加载的图像。相反,在正文中直接使用PHP(而不是通过CSS
url()
)添加图像。感谢您的回复。我不知道该怎么做……你觉得你能多解释一点或者分享一个参考资料吗?再次感谢。您可以在url后添加随机字符串(如时间戳)以防止缓存。比如:
…rotate.php?t=13464794645
确实可以,但是您打算如何在.css文件中实现这一点?至少我想不出一个办法来实现它。谢谢你的建议。我试过了,但没有效果。
    body.page-boxed-bg {
    background-image: url(https://www.website.com/wp-content/themes/theme-child/images/backgrounds/rotate.php);
}