Using NextGEN gallery to bring an image gallery to your WordPress blog
Bringing a customized image gallery isn’t as easy as it should be. Many developers have tried to solve this problem, and in my opinion Alex Rabe has done an excellent job of doing so. He has developed a WordPress plugin called NextGEN Galllery which allows the author to easily update and manage photos, and (this is important) allow for almost complete customization on how the images are displayed. As a specific example on how to do this, I’m going to walk you through getting NextGen gallery and the awesome Galleria jQuery image gallery to get along.
Once you have the NextGen plugin installed and customized, it’s time to begin getting the gallery to display correctly using a custom template. Alex talks a bit about templates, but I don’t think there is enough information for beginning. The first step with nextGEN templating is to create the folder nggallery at the root of your theme’s directory. I like to start off with the plugin’s default gallery file when creating a template. To do that copy the file gallery.php from the nextGEN plugin directory to your newly created nggallery folder. Gallery.php can be found here:
wp-content/plugins/nextgen-gallery/view/gallery.php
You will see the structure of the default gallery in gallery.php, and it will definitely need some changes in order to work with galleria. Galleria needs an unordered list structure for the thumbnails and a div container for the large image. The most important part of gallery.php is the foreach loop that cycles through all the images and outputs the html to display. This is how it looks now:
<?php foreach ( $images as $image ) : ?>
<div id="ngg-image-<?php echo $image->pid ?>" <?php echo $image->style ?> >
<div >
<a href="<?php echo $image->imageURL ?>" title="<?php echo $image->description ?>" <?php echo $image->thumbcode ?> >
<?php if ( !$image->hidden ) { ?>
<img title="<?php echo $image->alttext ?>" alt="<?php echo $image->alttext ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
<?php } ?>
</a>
</div>
</div>
<?php if ( $image->hidden ) continue; ?>
<?php if ( $gallery->columns > 0 && ++$i % $gallery->columns == 0 ) { ?>
<br style="clear: both" />
<?php } ?>
<?php endforeach; ?>
We need to change it to output the proper html for galleria like so:
<?php foreach ( $images as $image ) : ?>
<?php
$imgCounter = 0;
if ( !$image->hidden ) { ?>
<li<?php if ($imgCounter == 0) : ?><?php endif; ?>><img title="<?php echo $image->alttext ?>" alt="<?php echo $image->alttext ?>" src="<?php echo $image->url ?>" /></li>
<?php } ?>
<?php if ( $image->hidden ) continue; ?>
<?php $imgCounter++;?>
<?php endforeach; ?>
Notice how I used $image->alttext and $image->url. With knowing these basic properties, you can pretty much create any html your gallery plugin requires. Here is the full gallery.php file I modified:
<?php
/**
Template Page for the gallery overview
Follow variables are useable :
$gallery : Contain all about the gallery
$images : Contain all images, path, title
$pagination : Contain the pagination content
You can check the content when you insert the tag <?php var_dump($variable) ?>
If you would like to show the timestamp of the image ,you can use <?php echo $exif['created_timestamp'] ?>
**/
?>
<?php if (!defined ('ABSPATH')) die ('No direct access allowed'); ?><?php if (!empty ($gallery)) : ?>
<?php if (is_single()) : //only call full gallery if on single page ?>
<!-- Thumbnails -->
<div></div> <!-- container for main image -->
<div>
<!-- <ul> -->
<ul>
<?php foreach ( $images as $image ) : ?>
<?php
$imgCounter = 0;
if ( !$image->hidden ) { ?>
<li<?php if ($imgCounter == 0) : ?><?php endif; ?>><img title="<?php echo $image->alttext ?>" alt="<?php echo $image->alttext ?>" src="<?php echo $image->url ?>" /></li>
<?php } ?>
<?php if ( $image->hidden ) continue; ?>
<?php $imgCounter++;?>
<?php endforeach; ?>
</ul>
</div>
<!-- Pagination -->
<?php echo $pagination ?>
<?php else : //if on post list then display single image ?>
<div>
<img title="<?php echo $current->alttext ?>" alt="<?php echo $current->alttext ?>" src="<?php echo $current->url; ?>" />
</div>
<?php endif; //list?>
<?php endif; ?>
You will need the accompanying javascript and css that go along with the plugin. Ideally, you only want to call this when a gallery is being displayed. I haven’t found a good way of doing this without editing nggallery’s files directly. Instead, at least for now, I add these files to the appropriate template files. In my case, I only needed to add them to single.php since the galleries are only being shown when a single post is being displayed. If anyone has an idea of how to limit these files to only when they’re needed, please let me know.
Now all you need to do is call the gallery in your blog post. This is done with the nggallery shortcode. Grab the id of the gallery you created, and throw this shortcode:
[nggallery id=2]
You do have the option to have multiple gallery templates. Just create a new php file in your nggallery folder. Start the name of the file as gallery-, like gallery-example1. Then to use this non-default gallery, call it like this within your post:
[nggallery id =1 template=example1]





This is great!
Could you give me the example how it looks like on the web page?
Tom, here is a site that I’m working on that uses this technique. http://adam.litesinteractive.com/2010/01/hello-world-3/
Finally someone trying to simplify the instructions for making NextGen galleries more customizable! Thank you!!!
I am so new to wordpress.org and to css and php so my terminolgy might be a little off… I’m using NextGen for my galleries and working on a new site that isn’t at all ready to go public yet.
I want my galleries to look like the galleria demo you gave above (Galleria jQuery image gallery*) I understand the creation of the new custom template and how to call that out on my page etc.
BUT I do not know how to customize the php file to make my galleries look just like the demo from galleria. HELP!…..
Here’s a link to a gallery in my site as it looks now using NextGen defaults (the rest of the site is a mess still)
http://wp.merrilyrollingalong.com/gallery/jalama-beach/
Also do the files galleria.css and jquery.galleria.js go in the Plugins folder?
Thank you!!!
Stacey
*http://www.monc.se/kitchen/146/galleria-a-javascript-image-gallery
In my case I combined my gallery styles with my main stylesheet. For the javascript file I called that in my single.php file. In your case, it looks like it would be in your page.php file. I called the js file using the wp_enqueue_script call.
The template file is a bit more tricky. The example I provided can basically be broken up into two main sections. First, line 17 – 22 builds the container for the large image. Lines 23 – 28 are pulling each thumbnail in.
Hope this helps.
Hay Eric,
great you wrote some info for using Galleria on WordPress!
I tried it, and got a little question;
I completed making a gallery on my wordpress, but i don’t understand how i now can style it the way i like..
I got the plugins installed
(folder “galleria-wp” and “nextgen-gallery” are in my plugin-folder).
Is it correct that my “nggallery-folder” is placed in the “themes-folder”?
How to intergrate the Galleria-style now?
Thx in advance!
Oh, my wordpress is still in another folder: iamheinz.be/hz
Did you figure everything out?
Nope, but I used another gallery-plugin :)
thx anywayz
Hi there!
Being a wordpress and nextgen newbie I’m not quite getting the second part of implementation. Using your modified gallery.php file which should make a for each img within doesn’t do exactly that. Looking at source it just returns first img within an album. I would also highy appreciate a more precise explanation where to put link references to .css and .js (I included jquery via wp_enqueue_script). Thanks in advance for your reply.
My code only returns the full gallery when you are in a single post, not the post listing. I did that to save on tons of client-side processing, and, more importantly galleria only allows for one gallery per page.
Unfortunately I don’t have a great solution for you on the js and css placement. I put mine on the single.php file for my client so the js and css will be loaded regardless of whether or not you are using a gallery on that post. You could create a separate template file for gallery pages and call that template within your post or use shortcodes. I think that with WordPress 3.0 we will have much more flexibility with this.
Yes I know galleria only allows for one gallery per page. The thing is I put all of css and js for galleria including jquery (this time a direct call over wp_enqueue_script) in page.php because I tend to use WP for cms eg. preferring pages over posts. When I look at the source code it’s identical to galleria demo html source code but somehow it doesn’t make the thumbnails and it doesn’t show an active img in . I named all id’s and classes. Any ideas? Thanks!
Hi. I’m currently using Nextgen Gallery with the flashviewer plugin but would like to switch to Galleria. Any possibility you could set up a “for dummies” style tutorial to get the basics set up with the templates included in the Galleria download (http://github.com/aino/galleria)? I’m very un-technical.
Thanks.
Hi, this is slightly off topic, but I’m looking for a way to use multiple .css files, one per template. I find it easier than adding custom classes to nggallery.css each time (assuming this is the way to go). Is there a way to do this? Am I missing the obvious? TIA.
working very very hard to use NextGEN gallery+Galleria in WordPress. It must be helpful, but… :http://bit.ly/cpuvX7
This comment was originally posted on Twitter