NextGen Plugin – Display Gallery Image Count

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:

Read More
<a rel="prettyPhoto" href="<?php echo $image->imageURL ?>" 
<?php $image->thumbcode ?>><span>view</span></a> <?php echo $total_images; ?> pictures

Anybody have any tips?

Thanks,
Ian

Related posts

Leave a Reply

4 comments

  1. I used this little hack to get my gallery id from the shortcode I pasted inside my post ([nggallery=1])

    <?php
        $id = get_the_ID();//Get the id of the specific post (inside the loop)
        $string = get_the_content();//You get the whole post content where in the end of it lies your shortcode (for example [nggallery=1])
        if(preg_match_all('/=(.*?)]/s',$string,$match)) {            
        $finalstring=$match[1][0];
        }// get the id only (gets all chars after '=' and before ']')
        global $wpdb;
        $total_attachments = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggpictures WHERE galleryid=$finalstring" ); // Voila!
    ?>
    
  2. Hope my solution can work for you.

    This is the code you need to count the images:

    $global $wpdb;
    $images    = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggpictures") );
    $galleries = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggallery") );
    $albums    = intval( $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->nggalbum") );
    
  3. The answer is you can’t.
    You can count the keys in the $images variable but it will return the wrong number.
    You can retrieve the total number of all the images, or all the galleries which is useless.
    You can even use a counter as you cycle through the array to try to count how many images are in your gallery, but you’ll still get the wrong number.
    You can try to use the global variables tthere by default: images->total, which are empty and undefined and don’t even exist in the $images or $current object.

    The code displays a (“Picture 3 of 7)” type display. If you use it in the imagebrowser.php template, it works. If you paste this exact same code in the gallery-carousel.php template. It will not work. Now you might think switching to $current->total would work, since it inexplicably does for the rest of the variables when using gallery_carousel, but no, it does not. The answer is you have to pull the info from the database directly.

     <?php _e('Picture', 'nggallery') ?> <?php echo $image->number ?> <?php _e('of',      'nggallery')?> <?php echo $image->total ?>
    

    It is a sinister, sinister plugin.

  4. If you can’t count the images on a gallery, you can count the tags on html block.

    On my theme, I get the gallery ID from an custom field and echo the gallery inside the template. So, it’s something like that:

    <?php
        $myGalleryId = 1; // example
        $displayImages = 0; // the number of images you want to display from gallery. 0 = all images 
        $newnggShortcodes = new NextGEN_Shortcodes;
        $htmlGallery = str_get_html($newnggShortcodes->show_gallery( array("id"=>$myGalleryId,"images"=>$displayImages,"template"=>"popular") ));
        $countImg = count($htmlGallery->find('img')); // number of total <img> tags inside the gallery
        $str = $htmlGallery;
    ?>
    <!-- now the html -->
    <div id="myGallery">
    <?php echo $str; ?>
    </div>
    <span>Total images: <?php echo $countImg; ?></span>
    

    Hope it helps.