在帖子类型的模板中,我有一个自定义标题部分,我想在其中显示多个背景图像。这是单个图像的当前代码:
<div class="single__header" style="background-image: url(../content/uploads/2018/04/fundings-header.jpg);
background-repeat: no-repeat;
background-position: center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;">
如何使用/加载多个网址,随机显示不同的背景图片?经过多次搜索,我找到了this solution,但不知怎的,我无法让它发挥作用。
有什么建议吗?
答案 0 :(得分:0)
不需要额外的php文件。 这是一个更优雅的解决方案:
<?php
$imagesDir = 'images/'; //path to your images directory
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$randomImage = $images[array_rand($images)];
?>
<div class="single__header" style="background-image: url(<?= $randomImage ?>);
background-repeat: no-repeat;
background-position: center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;">
Test
</div>