WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using the NextGen gallery WordPress plugin for a site. In my gallery.php template I want to retrieve the number of images for each gallery displayed in the loop. I cannot figure out a way to get the data and print it under the thumbnail of each gallery that's called in gallery.php

Here is where I want to insert the gallery image count and print it:

global $wpdb; 
$images = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggpictures") ); ?> 
<a rel="prettyPhoto" href="<?php echo $image->imageURL ?>" <?php $image->thumbcode ?>>
<?php echo $images; ?> pictures
share|improve this question
I remember using custom templates for NextGen, can you not just count() the images in the array? – Cristian Nov 19 '12 at 17:43
Hello Christian, Yes, I am using a custom template for the gallery. I just can't get each gallery displayed with the code above to display it for each array. Advice? – Ian McAbee Nov 20 '12 at 6:10
Do you want a single page with a link to each gallery? If so I would suggest using the Album feature, as I think that's exactly what you want. – Cristian Nov 20 '12 at 11:29
Yeah, I like the album feature and how it counts the number of galleries. I just need that sort of function for images per gallery. So it would print the number of images in a gallery. – Ian McAbee Nov 20 '12 at 14:31
If you are outputting a gallery then there is a global called $images just use echo count( $images ). However if you are listing all galleries without being in an albumn and want to show the number of images I will have to do some more digging. – Cristian Nov 20 '12 at 16:03
up vote 0 down vote accepted

Using your above code, you were just missing the WHERE clause. Hopefully that should work.

global $wpdb; 
$images = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggpictures WHERE galleryid = {$gallery->ID}") ); ?> 
<a rel="prettyPhoto" href="<?php echo $image->imageURL ?>" <?php $image->thumbcode ?>>
<?php echo $images; ?> pictures

It's not the best way to implement it, but if it works for you :)

share|improve this answer
Cristian you are a saint, thank you sir! – Ian McAbee Nov 27 '12 at 4:34
if ( $postid !='' ) {
    $set = ci_featgal_get_attachments();
    $i = 0;
    while ( $set->have_posts() ) : $set->the_post();
        get_template_part( 'loop' ); 
    ?><h6 style="font-size:10px;color:#c9b07d;"><?php echo $i ." PHOTOS";?></h6><?php
share|improve this answer
Please file an edit and explain why this would work. – Gabriel Sep 19 '15 at 16:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.