Post navigation

code snippets

WordPress function for an unordered list of random images from a NextGEN Gallery

I was having trouble getting the NextGEN gallery shortcode to output five random images onto a page without its own formatting, so I wrote this function to simplify it.

This code goes into your functions.php file…

function ngg_random($howmany, $galleryID) {

    // Fetch the required number of random images
    $imageArray = nggdb::get_random_images($howmany, $galleryID);

    $i = 0;
    $result = "<ul>";

    // Array loop
    foreach ($imageArray as $image) {

		$i++;
		$result .= '<li class="nggimage img-'.$i.'"><img src="'.$image->imageURL.'" title="'.$image->alttext.'" alt="'.$image->description.'" /></li>';

	};

    $result .= "</ul>";

    echo($result);
}

And you can call it in a template file using this code…

<?php ngg_random (5,2); ?>

Where the first digit is the number of random images needed and the second is the Gallery ID.

About David Goodman

Leave a Reply

Your email address will not be published. Required fields are marked *

Please enable javascript to post a comment !