Explore » Food & Cooking » | Shopping » | Health & Fitness » | Beauty »

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.

In nggwidget.php:

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.


Previous: Los Angeles Wildfires

Next: Early Morning Dog Catcher

Tags: , ,



Other resources:


  • Hi, im using NextGen Smooth Gallery, will it work?
  • Mike
    Hello, I'm using the function:

    if (function_exists ( "nggDisplayRecentImages"))
    nggDisplayRecentImages (1);

    I would like the image were at 100% and not the size of the thumbnail.
    How can I do?
    Thanks
  • Mike
    Hello, I'm using the function:

    I would like the image were at 100% and not the size of the thumbnail.
    How can I do?
    Thanks
  • robert
    that was quick ;) now it is working, cheers!
  • @Robert

    I don't know what version of NextGen you have, this entry is for 0.99.1. The widget allows you to specify which galleries to pull pictures from. If you're doing a manual call, follow what I detailed above and set "allow" for $exclude and for $list should be your gallery IDs such as "5,9"
  • robert
    hi, I have 4 galleries but I want to use pics just from two of them, how can I do that?
  • Vuthy
    @Kenneth all I can think of is to use some PHP if-else statement to determine if it's the first image, then apply such-and-such class, but that's beyond the realm of my knowledge. Good luck!
  • Kenneth
    hi! I'm looking for a way to ad a specific css-code for each nggDisplayRandomImages I uses, as it is now nggDisplayRandomImages output them as smaller thumbs, but I want one of the nggDisplayRandomImages to show them as larger pictures. Any idea?
  • Any one know how I can get this to work as a background image in my header?
  • Vuthy
    I should update the blog to clarify, but this entry was written for an older version of NextGen (0.99.1). There have been many changes and updates since then (1.3.5 is current as of this comment).

    I haven't upgraded because if it ain't broke, why risk breaking it? :-)
  • doran
    Wonderful documentation of the parameters, but as jonnycobra pointed out:

    e.g. nggDisplayRandomImages("10","130","86","allow","5");

    .. works perfectly here, displaying random images of gallery 5. No need to modify the original code.
  • Hi, I have been looking EVERYWHERE for the answer to this question, with no luck - can you allow people looking at your gallery to comment on individual photos? I absolutely love NextGen, but I just can't figure out how to get the comments enabled... Or IF I can.... I would really appreciate your help. I'd hate to have to switch to another gallery system and have to move and re-title/tag all my photos. 0_-

    <abbr>Mandy recently blogged about: 52 Pick-up</abbr>
  • Hi Vuthy,
    I was hoping you could help me out with another little hack, i've been playing around myself, but no luck.
    I have the widget in the sidebar bringing up random images, but i would love for those images to link back to the album its from, not the image path. In fact I am trying to figure out how to stop the images in the gallery linking back to the image path as well. So annoying it sends people off the site. Any help would be very much appreciatted. thanks :)
  • sorry about that, the code snips in my last post disappeared...when calling nggDisplayRandomImages, you must specify all the parameters in quotes (double or single): nggRandomImages('6', '110', '110', 'allow', '6').
  • Thanks for info, it was very useful in discovering the ngg random image function. However, I don't believe its necessary to create a custom function to accomplish this.

    The reason '' won't work is because the parameters (which should be string data types) are not in quotes. When the parameters aren't correct, the parameters maintain their default values specified in the function definition (e.g. $height="300").

    For my application, simply using worked perfect.
  • interconnect
    hi, thanks for the tip! i'm doing the same thing as you and editing my template manually. however, i'm looking for a way that i could have let's say 4 random images displayed in a row, instead of 2 on one line and 2 below that. do you know how to do this?
  • Michael
    I noticed when I used your code in my sidebar, that the resulting images displayed in the sidebar do not have any css styling whereas the images displayed in the sidebar via the widget, do have styling (border and slightly different spacing). How would I get that using just straight php as your example for the sidebar code above shows?
  • Fantastic tip! Thank you for posting it. Was about to start tearing out my hair!

    <abbr>Mentalacrobatics recently blogged about: Transcript of webchat with US Ambassador to Kenya</abbr>
  • Alexwebmaster
    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru
  • Working great - thanks so much for working through this solutions!
  • Jim
    Thanks.... sorry for the delay....
    Let me rephrase my problem...
    As I mentioned
    "I have a general page (say Gallery) which includes all albums on the site.. [album id= template=extend]".
    By default this page shows Galleries inside Albums... it would be nice to show only Albums... .
  • Vuthy
    Hi Jim, if I understand correctly, you want to prevent a gallery (one you named Intro) from showing up on a specific page?

    If this is correct, all you have to do is not associate that gallery with the album being called. Did you drag and drop that gallery into the album? You don't have to associate a gallery with any album. For example, my photo gallery page is an album that shows some but not all of my galleries. I have other galleries that I created but I didn't associate them with my album so those galleries will not show up on that page.

    As for your second question, I have no idea. :-S
  • jIM
    The other approach (which I like better) is to create slide show based on tags (without using the INTRO category) which might solve the problem.....but due to my general gallery settings to show gallery first..... i cant override this future with ...ngg_gal_ShowOrder -->Show first the slideshow.
  • jIM
    I might be out of topic here:) but what I am really asking is :
    I have created a gallery (say Intro) which slideshow I want to be shown on Home Page. Also, I have created several albums which contains a lot of galleries. Now I have a general page (say Gallery) which includes all albums on the site.. [album id= template=extend] . But I dont want the gallery (Intro) to be shown on this page.
    Any ideas?
  • Vuthy
    @Jim

    Hi, I don't really understand your question. This tutorial *is* about how to show a specific gallery. In my example, I set it to allow the gallery with an id of 1. To show all galleries except for a specific one, I would have instead given the exclude parameter a value of "denied" instead of "allow."

    If this is what you're asking, then it's exactly what the entry is about. If your sidebar uses widgets, then you can do this easily with the widget.

    However, if you are indeed asking about something else, I'll need your help to try and understand your question better.
  • jIM
    Hi,
    I was wondering if it is possible to show only specific gallerys (eg id = 2,3,4,5,6). Or exclude the gallery 1 ?
  • Loren
    Thank you! That was exactly what I was looking for. I had expected that there would be a built in function that would do this but I could not find one. I use it in my page templates to pull up an image as a page header.
  • Prashant
    (L) (L) (L) (L) (L) (L) (L) (L) (L) (L) (L) (L) (L) (L)
blog comments powered by Disqus
Categories

EventCalendar:

November 2008
Sun Mon Tue Wed Thu Fri Sat
 1
2345678
9101112131415
16171819202122
23242526272829
30EC
    • No events.
Dugg:

Clicky Web Analytics