NextGen Gallery: Random Photos from a Specific Gallery
Please note: The following was written for NextGen version 0.99.1 and Wordpress 2.6.3. Depending on your version of NextGen and/or Wordpress, there may be slight to significant variances and this issue may have been fixed in newer releases.
This is a detailed discussion of why the original manual function call doesn’t work (widget works) and how I ended up fixing it. Click the following link if you want to skip all the explanation: how to manually display a random or recent photo from a specific NextGen gallery in your Wordpress template. If you need the basics on getting NextGEN to work and show your photos, click here for a quick guide to common NextGEN codes.
###
I really like the NextGEN photo gallery plugin for Wordpress, despite its lack of documentation and some confusing organization.
For example, in NextGEN you create what they call an “album,” which is a collection of one or more galleries. Galleries contain your photos. To me, it makes more sense to call the first part the “gallery” and your collection of photos should be called “albums,” but they have it backwards. I also don’t see a reason why you would need to create more than one NextGEN album. I think the confusion arises because English is not the programmer’s first language, but with that aside, it is still the best photo gallery plugin I’ve come across for Wordpress.
This post is about how to pull a random image from a specific category. It’s something I wanted to do tonight on my About page and I didn’t see anything about it on NextGEN’s official website or on Google. However, my numerous Google searched revealed that a lot of people have been trying to do the same thing, looking for help on forums and in Wordpress with no luck. I decided to give it a crack and was successful after some trial and error. So for others who may be in the same boat as I was, here’s how to display a random photo from a specific gallery in NextGEN anywhere in your template. This can be adapted to pull recent photos also.
In case you don’t know, you can show random or recent photos using the NextGEN widget. My theme doesn’t use Wordpress widgets, so in my sidebar is the manual code that the programmer provided. The code for random images is:
<?php
if (function_exists("nggDisplayRandomImages"))
{ nggDisplayRandomImages($number_of_pic,$Width,$Height); }
?>
It can be simplified to:
<?php nggDisplayRandomImages($number_of_pic,$Width,$Height); ?>
In my sidebar, I have 6 thumbnails that are 50 x 50, so my sidebar code is:
<?php nggDisplayRandomImages(6,50,50); ?>
This was my starting point. I figured if the widget allowed you to restrict the photos to a specific gallery, the manual code should do the same. I opened up the nggwidget.php file (found in /wp-content/plugins/nextgen-gallery/) and looked at the code. I did a search for the function being called: nggDisplayRandomImages. It’s near the bottom of the page and I saw that the function had several settings:
function nggDisplayRandomImages($number, $width = "75", $height = "50", $exclude = "all", $list = "")
{
$options[1] = array('title'=>'',
'items'=>$number,
'show'=>'thumbnail' ,
'type'=>'random',
'width'=>$width,
'height'=>$height,
'exclude'=>'all',
'list'=>$list );
nggWidget::ngg_widget_output($args = array(), 1, $options);
}
What this means is that when you put the code in your template, in the parentheses you can set 5 options. The order is very important.
nggDisplayRandomImages($number, $width, $height, $exclude, $list)
$number: the number of pics you want to display, there is no default, so you have to provide this
$width: the width of each picture, defaults to 75 if you don’t provide anything
$height: the height of each picture, defaults to 50 if you don’t provide anything
$exclude: gallery restriction type
all means include all galleries
denied means deny the galleries in $list
allow means only allow the galleries in $list
In the code, it defaults to all but this means nothing because it is hard-coded to submit “all” in the array
$list: gallery IDs affected by your $exclude, defaults to blank but it doesn’t matter since $exclude is hard-coded
These are other settings that you can’t seem to set when you call the function but is hard-coded and sent in the array:
’show’ => ‘thumbnail’
This is the image file being shown, either the thumbnail or the original image
‘type’ => ‘random’
Either random or recent pictures
Because the exclude is hard-coded, it won’t work if you were to try and do:
<?php nggDisplayRandomImages(1,50,50,allow,3); ?>
That above code is trying to say show 1 random image, 50px by 50px, and only allow an image from the gallery with an ID of 3. But since $exclude has been hard-coded in nggwidget.php to “all” the function ends up still choosing from all galleries.
So the function needs to be changed, but because I use the function as-is for my sidebar, changing it would affect my sidebar display. What to do? Copy, paste, and modify.
I copied the entire nggDisplayRandomImages function and made certain changes to suit my needs:
function nggDisplayRandomImage_Vuthy
($number = "1", $width = "300", $height = "300", $exclude = "allow", $list = "1")
{
$options[2] = array('title'=>'',
'items'=>$number,
'show'=>'orginal' ,
'type'=>'random',
'width'=>$width,
'height'=>$height,
'exclude'=>$exclude,
'list'=>$list );
nggWidget::ngg_widget_output($args = array(), 2, $options);
}
As you can see, I gave it a new name so when I want to display it in my template, I’m using the following code:
<?php nggDisplayRandomImage_Vuthy(); ?>
Since I’m only going to use this for one particular thing, I set a few defaults so I don’t have to enter it into the function call in my template. I removed the hard-coded items from the array so it sends either my defaults or what I specific in the function call in my template. Plus I changed the show type to ‘orginal’ because I’m displaying a 300 x 300 image and would rather have it shrink my full-size image file than stretch out my thumbnail file. And yes, the programmer spelled original wrong; the correct spelling won’t work. You can also set it up so you can specify the setting for ’show’ each time you call the function, but it wasn’t necessary for my needs.
I’ll be the first to admit that I just a hack. I learn by trial and error, changing something and seeing what was affected. Don’t fully understand all this PHP, but this works and that’s good enough for me.
Update: I wrote a guide to creating albums and galleries in NextGEN.
Next: Early Morning Dog Catcher
Other resources:
Stay updated! Sign up for email alerts whenever a new entry is posted
Add this blog feed to your reader »
Quick Guide to NextGEN Gallery Common Codes
Guide to NextGEN Gallery Plugin: Album and Gallery
How to Change Default Full Size Image for Wordpress

Latest Tweets

