I have found a solution. After a bunch of searching around I found some code that determines if a post has a specific short code in it.
That code also has some attempt to parse the parameters to the short code. Which is good because I want the gallery ID. There were some issues with the code as posted so I tweaked it. Here is code that can find a short code in a post and get its parameters:
<?php // Look for a NextGEN gallery
$galleryID;
$previewIndex = 1;
$regex_pattern = get_shortcode_regex();
preg_match ('/'.$regex_pattern.'/s', $post->post_content, $regex_matches);
if ($regex_matches[2] == 'nggallery') :
// Found a NextGEN gallery find out what ID
// Turn the attributes into a URL parm string
$attribureStr = str_replace (" ", "&", trim ($regex_matches[3]));
$attribureStr = str_replace ('"', '', $attribureStr);
// Parse the attributes
$defaults = array (
'preview' => '1',
);
$attributes = wp_parse_args ($attribureStr, $defaults);
if (isset ($attributes["id"])) :
$galleryID = $attributes["id"];
endif;
if (isset($attributes["preview"])) :
$previewIndex = $attributes["preview"];
endif;
endif;
?>
What needed to be tweaked was the handling of the parameters. Using trim instead of some secret charter and switching to wp_parse_args
to correctly handle the short code parameters. Once the above code finishes executing in a WP loop $galleryID
will hold the NextGEN gallery ID and previewIndex
will be set to the preview index or 1 if no previewIndex
attribute was present.
previewIndex
is an attribute I "added" to indicate what thumbnail to use for the gallery preview. NextGEN ignores it and the galleries render as normal but now I can use it for my Theme to display a specific icon in the preview entry.
Here is the code from my loop-index.php
and loop-category.php
that handles creating the gallery preview:
<?php /* Enhance the content preview with an image from the NextGEN gallery */ ?>
<?php
global $nggdb;
$gallery = $nggdb->get_gallery ($galleryID, 'sortorder', 'ASC', true, 0, 0);
$image = $gallery[$previewIndex];
$total_images = count ($gallery);
?>
<?php if (isset($image) && isset($image->thumbURL)) : ?>
<?php /* Show the thumbnail */ ?>
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
<img class="entry-preview-image" src="<?php echo $image->thumbURL ?>" align="left" />
</a>
<?php endif; ?>
<?php /* Show the text excerpt */ ?>
<?php the_excerpt(); ?>
<?php /* Show the statement of number of images contained */ ?>
<em><?php printf( _n( 'This gallery contains <a %1$s>%2$s photo</a>.', 'This gallery contains <a %1$s>%2$s photos</a>.', $total_images, 'twentyten' ),
'href="' . get_permalink() . '" title="' . sprintf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ) . '" rel="bookmark"',
number_format_i18n( $total_images )); ?>
</em>
<?php endif; ?>
This makes use of information from another answer regarding accessing NextGEN gallery objects in order to get the thumbnail and the count of images in the gallery.