Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Zufallsbild
<?php
// Allgemeine PHP-Variante
add_shortcode( 'randompic', 'rad_randompic' );
$vn = "./static-img/randompic";
$html_vn = "static-img/randompic/";
$barray = array(1,2,3,4,5);
$verzeichnis = opendir($vn);
while($file = readdir($verzeichnis)) {
$s = @getimagesize($vn."/".$file);
if(in_array($s[2], $barray))
$auswahl[] = $file;
}
mt_srand((double)microtime()*1000000);
$number = mt_rand(0,count($auswahl)-1);
$ausgabe = "<img src='".$html_vn.$auswahl[$number]."'>";
return $ausgabe;
?>
<?php
// Variante mit Funktion und Shortcode für Wordpress
add_shortcode( 'randompic', 'rad_randompic' );
function rad_randompic() {
$vn = "./static-img/randompic";
$html_vn = "static-img/randompic/";
$barray = array(1,2,3,4,5);
$verzeichnis = opendir($vn);
while($file = readdir($verzeichnis)) {
$s = @getimagesize($vn."/".$file);
if(in_array($s[2], $barray))
$auswahl[] = $file;
}
mt_srand((double)microtime()*1000000);
$number = mt_rand(0,count($auswahl)-1);
$ausgabe = "<img src='".$html_vn.$auswahl[$number]."'>";
return $ausgabe;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.