Wordpress Themes - WP Forum at BFA

Wordpress Themes - WP Forum at BFA (http://forum.bytesforall.com/index.php)
-   Montezuma Theme (http://forum.bytesforall.com/forumdisplay.php?f=53)
-   -   How to: use Random Images (http://forum.bytesforall.com/showthread.php?t=19455)

jerryc Jan 25, 2013 02:50 PM

How to: use Random Images
 
Note: This may not seem like a Montezuma tip at first, but the output uses a nested Montezuma grid for the layout. I know of no other theme that this would work with so easily.

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:
  1. Sets the pool of items from which to randomize. In this example, the pool is all the image files in a certain folder.

  2. 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.)

  3. Randomizes the array. In the example, the filenames are randomized.

  4. Selects, for use, the first n items from the array.

  5. Discards the selected items from the array, so that they are not repeated later.
Assumptions for images:
  1. You have ftp or some other access to your file system. This procedure does not use the wp media filesystem.

  2. 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;
}  

?>

Then, in the PHP sidebar widget, call the function with:

PHP Code:

<?php echo custom_make_random_thumbnail_row_6(); ?>

On the example page, I it's called twice, once at the top and once at the bottom of each page, and there are never any repeats.

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.


All times are GMT -6. The time now is 05:03 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.