Wordpress Themes - WP Forum at BFA
Click Here To DONATE! Support the theme development, get more attention and additional benefits based on your donation level. Your forum name will be highlighted.

Wordpress Themes - WP Forum at BFA » WordPress Themes » Montezuma Theme »

How to: use Random Images


Build custom WP THEMES, without
coding.
Click here for THEMEFRAME

From the author of Atahualpa, the #1 most
downloaded (700,000+) theme @ wordpress.org
  #1  
Old Jan 25, 2013, 02:50 PM
jerryc
 
367 posts · Oct 2012
Florida
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.

Last edited by jerryc; Mar 22, 2013 at 03:42 PM. Reason: Better Coding

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
will header images do Javascript rotation and crossfade with a random order of images adamslc8 Header configuration & styling 3 Oct 14, 2011 12:58 PM
Random featured images in a widget peri Sidebars & Widgets 0 Sep 10, 2011 09:25 AM
How to get the header images to rotate in a random fashion? ptuquoc Header configuration & styling 3 Jul 29, 2009 05:02 AM
Calling random images in the sidebar, similar to header cowgirlgeek Sidebars & Widgets 1 Jun 30, 2009 06:41 PM


All times are GMT -6. The time now is 11:07 PM.


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