これが尋ねられたのをまだ見たことがありません。
画像ライブラリのランダムな画像を使用する WordPress テーマの全画面背景を作成する際に問題が発生しています。複数のサイトで使えるPHP関数を書きたいので、コード内で単純にダイレクトパスが使えません。また、そのサイトにアップロードされた画像のみをプルするように、MultiSite で動作する必要があります。ここに私が取り組んでいるコードがあります:
背景 Div の 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.'"');
}
DIV に固定の画像の場所を指定すると、フルスクリーンの画像が表示されるため、CSS マークアップが正しいことはわかっています。誰かがそれを修正するために何をすべきか教えてもらえますか?