WebsiteBaker Community Forum

WebsiteBaker Support (2.8.x) => Droplets & Snippets => Topic started by: Bramus on April 16, 2007, 10:07:42 AM

Title: Random Image snippet for use in the Template index.php
Post by: Bramus on April 16, 2007, 10:07:42 AM
Since i was looking for the random image snippet for my site, and the one posted here
did not work in the index.php of my template i went searching for another. With some google job and php knowledge of myself i managed to modify an existing code and i did a test on my websites and it did work 100%!

Go to the page were you want your image, i assume some were in the index.php of your template folder. Paste the following code there.

Code: [Select]
<?php

// Total images that are in the folder set below
$total "11";

// The file type you want to use
$file_type ".jpg";

// The location of your random images
$image_folder "../media/random";

// No editing needed below this

$start "1";

$random mt_rand($start$total);

$image_name $random $file_type;

echo 
"<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";

?>


Remember to name your images as: 1.gif - 2.gif and so on, or 1.jpg - 2.jpg and so on.

You can also make a different php page like randomimage.php and use the include function, but i didn't test that.

Just using the code above in your index.php of your template should do the job.

Comments are always welcome!


Greetz BramuS
Title: Re: Random Image snippet for use in the Template index.php
Post by: janjoensen on May 03, 2008, 08:40:37 PM
How can I get this with out border and automatic random?
Title: Re: Random Image snippet for use in the Template index.php
Post by: marathoner on May 04, 2008, 03:11:41 AM
Border is controlled via CSS. Just style the IMG tag, or whatever tag you are referring to, without a border.

I don't know what you mean by without automatic random since this is a random image snippet.
Title: Re: Random Image snippet for use in the Template index.php
Post by: Danny on July 28, 2008, 03:14:30 PM
Thx 4 sharing your work Bramus!

I had a problem with sub-menus and/or multilanguage versions of websites.
Easy fix for it with the Template_dir function of wbb.

Code: [Select]
<?php
$total 
"6";
$file_type ".jpg";
$dir TEMPLATE_DIR// get template-dir 

$image_folder "img/random";

$start "1";
$random mt_rand($start$total);
$image_name $random $file_type;

$filename "$image_folder/$image_name";

print 
"<img src=\"$dir/$image_folder/$image_name\"  />";
?>