While the example code generates random images, it can be modified to generate random anything. The example is in the showing off thread. Since php code doesn't show up with firebug, I have posted the code it here for those who can learn something from it, or just use it as is.
Here's what the code does:
- Sets the pool of items from which to randomize. In this example, the pool is all the image files in a certain folder.
- Puts the names or some other reference to the pool items into an array. In the example, all the filenames in the folder are put into an array. (Because making an array of filenames also includes the file references ./ and ../, those are deleted before the randomization.)
- Randomizes the array. In the example, the filenames are randomized.
- Selects, for use, the first n items from the array.
- Discards the selected items from the array, so that they are not repeated later.
- You have ftp or some other access to your file system. This procedure does not use the wp media filesystem.
- If you use two different folders, one for thumbnails and one for full sized images, they have the same filenames and there is a one to one match for each, one in each folder.
Two plugins do this. Here's the code that goes into a PHP code snippet: (If you use the snippet plugin, remove the opening and closing php tags.)
PHP Code:
<?php
/**
* Random thumbnail row 6 wide
*
* makes row of 6 random thumbnails from designated folder
* creates uniform alt text and no captions.
* For this code to work as written, all random thumbnail
* images must be in the same directory,
* and that directory must not contain any subdirectories.
* Image filenames must be the same as the thumbnails,
* and all in a different directory.
*/
/* Set path to domain files. For most servers, this will be the absolute path
* to the document root, plus the path from there to the domain name. If your
* server uses control panel, this can be found in the website area under CGI scripts,
* then php. Look for "Path to your Web document root:." Then add the path to your
* domain name on the end of that.
*
* Example: foo/bar/baz/public_html/your_domain_folder
*/
$custom_path = ("[your-path]");
/* Make key variables global so they can be accessed later
* Note: PHP documentation doesn't require this to be done outside
* of a function, but the process didn't work without doing it this way.
*/
global $custom_random_thumbnail_dir, $custom_random_image_dir, $custom_random_thumbnails;
/* Set path to image files, relative to the domain.
*
* Example: /images/gallery/thumbs
*/
$custom_random_thumbnail_dir = ("[your-thumbnail-directory]");
$custom_random_image_dir = ("/[your-image-directory]");
/* create alphabetical array of image names */
$custom_random_thumbnails = scandir("file:///" . $custom_path . $custom_random_thumbnail_dir);
/* remove results "./" and "../" from array */
array_shift($custom_random_thumbnails);
array_shift($custom_random_thumbnails);
/* randomize array*/
shuffle ($custom_random_thumbnails);
/**
* make random thumbnail row 6 wide
* @uses $custom_random_thumbnail_dir, $custom_random_image_dir, $custom_random_thumbnails
* @global $custom_random_thumbnail_dir, $custom_random_image_dir, $custom_random_thumbnails
* returns string html code for thumbnail row
*/
function custom_make_random_thumbnail_row_6()
{
global $custom_random_thumbnail_dir, $custom_random_image_dir, $custom_random_thumbnails;
$html = "\n\n<div class = \"row\">\n\n";
for($i = 0; $i <6; $i++)
{
$random_filename = array_shift($custom_random_thumbnails);
$random_thumbnail = $custom_random_thumbnail_dir . "/" . $random_filename;
$random_image = $custom_random_image_dir . "/" . $random_filename;
$html .= "<div class = \"col2\">\n";
$html .= "<a href = \"" . $random_image . "\" target = \"_blank\">\n";
$html .= "<img src = \"" . $random_thumbnail . "\" alt = \"thumbnail\">\n";
$html .= "</a>\n";
$html .= "</div>\n\n";
}
$html .= "</div>\n\n";
return $html;
}
?>
PHP Code:
<?php echo custom_make_random_thumbnail_row_6(); ?>
There may be more elegant ways of doing this using the WP media filesystem. With nextgen, as I understand it, if you use random images, it loads all of the images in the selected folder into the page, even the ones you don't see. With this procedure, only the n selected images are loaded to the page.
Update: Here's an example of a simple implementation of 3 random header images using just one plugin.