2009年12月2日 Tags:プラグイン
wordpress 写真をランダムに表示
【Random File】 写真などのファイルをランダムに表示
あるディレクトリ内にあるファイルをランダムに取得するプラグイン。
主に画像の表示に使用しますが、画像ファイル以外もランダムに取得することが可能です。
DL:coffee2code
参考サイト:いろいろなものをランダムに表示するWordPressプラグインの一覧
■コードサンプル
<?php
$dir = "/wp-content/photos/";
$prfx = "thumb_";
$dirlen = strlen($dir);
$prfxlen = strlen($prfx );
$syashin = c2c_random_file($dir);
if (substr($syashin,$dirlen,$prfxlen) != $prfx){
$origin = substr($syashin,$dirlen);
$thumb = $prfx.$origin ;
}
else{
$origin = substr($syashin,$dirlen + $prfxlen) ;
$thumb = substr($syashin,$dirlen);
}
?>
<a title="Photo Gallery" rel="lightbox" href="http://note.dizain.jp<?php echo $dir.$origin; ?>">
<img alt="logo" class="logo" src="http://note.dizain.jp<?php echo $dir.$thumb ; ?>" />
</a>
・$dir :
ランダムに表示したいファイルが格納されているディレクトリ名(前後に/が必要)
・$plfx :
サムネイル画像についている接頭辞
・$syashin :
ランダムに取得したファイル(値は /wp-content/photos/ファイル名 )
・substr($syashin,$dirlen,$prfxlen) :
文字列 $syashin のうち、ファイル名部分の先頭の文字を取得
取得したファイル名の先頭が$plfxでない場合はそのファイル名を拡大表示用に使い、そのファイル名の先頭に$plfxを付け足したものをサムネイル表示用にする。
取得したファイル名の先頭が$plfxの場合はそのファイル名をサムネイル表示用に使い、そのファイル名から$plfxをとったものを拡大表示用にする。
※ディレクトリ内にfoo.jpgとthumb_foo.jpgの二つのファイルがペアで存在することが前提です。

2010.01.23