函数随机化 - 我可以让它错过一个页面并且该页面上只有一个图像吗?可湿性粉剂


Function Randomize - Can I get this to miss a page and just have one image on that page? Wp

我在网站上工作,对于一个页面,我需要它只有一个图像,但这个函数一直搞砸了,有没有办法告诉它不能在一个页面上工作? 并仅显示一个背景? 这是代码。

<script type="text/javascript">function Randomize() {
var images = new Array("<?php bloginfo('template_directory'); ?>/images/bgone.jpg","<?php bloginfo('template_directory'); ?>/images/bgfour.jpg","<?php bloginfo('template_directory'); ?>/images/bgfive.jpg","<?php bloginfo('template_directory'); ?>/images/bgsix.jpg");
var imageNum = Math.floor(Math.random() * images.length);
document.getElementById("body-top").style.backgroundImage = "url('" + images[imageNum] + "')";}

我也为此使用 wordpress,这在标题中。

谢谢

这样的事情怎么样?根据页面名称拉取特定图像

如果您可以确定"页面",请使用基本名称,如果不能使用下面来获取完整的 url 进行测试

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

<?php
$page = basename($_SERVER['PHP_SELF']);
$src = '"' . bloginfo('template_directory') . '/images';
if($page == "Page_Name_Goes_Here")
{
  $images = $src . '/bgsix.jpg"'; // <-- image you want displayed on the 1 page
}
else 
{    
  $images  = $src . '/bgone.jpg",';
  $images .= $src . '/bgfour.jpg",';
  $images .= $src . '/bgfive.jpg",';
  $images .= $src . '/bgsix.jpg"';
}
?>
<script type="text/javascript">function Randomize() {
var images = new Array(<?php echo $images; ?>);
var imageNum = Math.floor(Math.random() * images.length);
document.getElementById("body-top").style.backgroundImage = "url('" + images[imageNum] + "')";}

输出:[当然我没有你的博客信息 fn(),所以不知道那里的输出]

显示在特定页面上

<script type="text/javascript">function Randomize() {
var images = new Array("/images/bgsix.jpg");
var imageNum = Math.floor(Math.random() * images.length);
document.getElementById("body-top").style.backgroundImage = "url('" + images[imageNum] + "')";}

未显示在特定页面上

<script type="text/javascript">function Randomize() {
var images = new Array("/images/bgone.jpg","/images/bgfour.jpg","/images/bgfive.jpg","/images/bgsix.jpg");
var imageNum = Math.floor(Math.random() * images.length);
document.getElementById("body-top").style.backgroundImage = "url('" + images[imageNum] + "')";}
相关文章: