Next Gen Gallery – Random Images in Widget

It’s been a while since I’ve had reason to deploy Alex Rabe’s fantastic Next Gen Gallery in a WordPress build. I’m currently producing Craft Town Scotland’s site and decided to use the Next Gen Slideshow widget for the blog sidebar. There is a bug which prevents the gallery using random images though, if you leave the gallery dropdown menu blank it will automatically start the slideshow from the newest gallery.

I had a quick look to see if I could spot it, but it’s a weighty plugin so rather than waste a lot of time I just created the widget object in the sidebar.php template. Doing it this way enables you to display the slideshow from a gallery by creating an array of all the gid values in the nggallery table, assigning a random value to a variable and then passing that value as an argument when instantiating a Slideshow Widget object. In this case I’ve hardcoded the image width and height values as they won’t change on the site.

<!-- blog sidebar -->
<?php if(!is_page()): ?>

<?php  
//We find how many gallery ids there are and override the $galleryID with a random number 
$numgall = $wpdb-&gt;get_results("SELECT gid FROM $wpdb-&gt;nggallery");
$array_size = sizeof($numgall);
$gall_gid = rand(0, ($array_size-1));
$galleryID = $numgall&#91;$gall_gid&#93;->gid;

echo nggSlideshowWidget::render_slideshow($galleryID, 210, 135);

<?php endif ?>
<!-- ENDS blog sidebar -->

Leave a Comment