Title: Gallery template, access shortcode attributes
Last modified: August 31, 2016

---

# Gallery template, access shortcode attributes

 *  [kimaldis](https://wordpress.org/support/users/kimaldis/)
 * (@kimaldis)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/)
 * Using this shortcode to insert a gallery into a post:
 * [nggallery id=1 template=templatename foo=123 fee=456]
 * Is it possible to pick up the two custom attributes foo and fee, or any other
   custom attributes, in the gallery-template.ph?
 * [https://wordpress.org/plugins/nextgen-gallery/](https://wordpress.org/plugins/nextgen-gallery/)

Viewing 15 replies - 1 through 15 (of 18 total)

1 [2](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/page/2/?output_format=md)

 *  Plugin Contributor [photocrati](https://wordpress.org/support/users/photocrati/)
 * (@photocrati)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227720)
 * [@kimaldis](https://wordpress.org/support/users/kimaldis/) – There are no current
   methods to randomly create a template “attribute” that can then be used by a 
   shortcode unless that attribute has a matching parameter in the shortcode itself.
 * Perhaps a clearer description of what you are wanting to display might help us
   direct you to a more “complete” shortcode?
 * There is also the following article related to NextGEN Gallery shortcodes you
   can find here: [http://imagely.com/docs/nextgen-gallery-shortcodes/](http://imagely.com/docs/nextgen-gallery-shortcodes/)
 * Thanks!
 * – Cais.
 *  Thread Starter [kimaldis](https://wordpress.org/support/users/kimaldis/)
 * (@kimaldis)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227724)
 * Sorry, I maybe didn’t explain myself that well. I’ve built a KA-template.php 
   template, placed it in the correct place in themes. I insert it into a post using:
 * [nggallery id=1 template=KA]
 * I would like to be able to have some control over, say, the number of columns
   displayed in the post by using an attribute in the shortcode:
 * [nggallery id=1 template=KA columns=5]
 * but there doesn’t seem to be a way to get at attributes in the shortcode from
   php code in the template file. Does that make more sense?
 *  Thread Starter [kimaldis](https://wordpress.org/support/users/kimaldis/)
 * (@kimaldis)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227805)
 * Mulling this over the weekend I couldn’t see any immediately obvious way to do
   this so I hacked a workaround. This code snippet searches for nggallery shortcodes
   in the post, checks the shortcode id against the gallery id handed to the template
   and pulls attributes from it if it matches. It will fall apart a bit if there
   are two galleries with the same id being shown in the page but I really can’t
   see that happening in a sensible world.
 *     ```
       global $post;
   
       $pattern = get_shortcode_regex();
       $thisGalleryID = $images[0]->galleryid;	// obscure place to get it but I can't find it in the rather badly documented $gallery object.
   
       // grep all nggallery shortcodes in the post.
       if (   preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches )
       	 	&& array_key_exists( 2, $matches )
       	 	&& in_array( 'nggallery', $matches[2] )
       	) {
   
       	$attributeStrings = $matches[3];				// an array of attribute strings, one for each shortcode found in the post.
       	$found = false;
       	foreach ($attributeStrings as $i => $attributes) {
   
       		$attrs = shortcode_parse_atts( $attributes );
   
       		$id = $attrs['id'] or false;
   
       		$columns 			= $attrs['columns'] 		or 5;
       		$info 				= $attrs['info'] 			or false;				// show the title banner to bottom of image
       		$infohover 			= $attrs['infohover'] 		or false;		// show/hide info bar on hover
       		$downloadable 		= $attrs['downloadable'] 	or false;
   
       		if ( $id == $thisGalleryID ) { 					// gid in the shortcode matches gid sent to this template?
       			$found = true;
       			break;
       		}
       	}
       	if ( !$found ) {
       		echo "couldn't find a shortcode match for this gallery ($thisGalleryID)<br>";
       	}
       }
       ```
   
 *  Plugin Contributor [photocrati](https://wordpress.org/support/users/photocrati/)
 * (@photocrati)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227814)
 * [@kimaldis](https://wordpress.org/support/users/kimaldis/) – Thanks for sharing
   your work-around … also without knowing / seeing what your custom template is
   doing, did you look at the `number_of_columns` shortcode parameter as well?
 * – Cais.
 *  Thread Starter [kimaldis](https://wordpress.org/support/users/kimaldis/)
 * (@kimaldis)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227816)
 * Any time.
 * I didn’t spot a columns count parameter so I added my own.
 * If I can be frank, I’m finding documentation at this level either sparse, sketchy
   or scattered. Things like the $gallery & $image objects seem to be largely undocumented
   and I can find nothing that resembles an API for NextGen. I’ve had to dig quite
   deep. I’m now looking into custom lightboxes and that’s proving interesting too.
   Maybe you have some links that I may have missed?
 *  Thread Starter [kimaldis](https://wordpress.org/support/users/kimaldis/)
 * (@kimaldis)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227817)
 * I still have features to add but have a working version of the template. It uses
   the Isotope packery plugin. It will have sharing, purchase buttons, it already
   has download buttons. Working example here, if you’re interested:
 *  Plugin Contributor [photocrati](https://wordpress.org/support/users/photocrati/)
 * (@photocrati)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227858)
 * [@kimaldis](https://wordpress.org/support/users/kimaldis/) – Updating our documentation
   is definitely on our list of things to get accomplished as we do recognize it
   is indeed “sparse” in several places.
 * An “undocumented” note when it comes to shortcode parameters, almost every element
   you can find via the browser’s developer tools in a specific Gallery Setting 
   can be used as a shortcode parameter for that gallery … just remove the display
   type reference.
 * For example: [http://take.ms/VGn95](http://take.ms/VGn95) shows … `photocrati-
   nextgen_basic_thumbnails_number_of_columns` et al.
 * So you would just use `number_of_columns` for the column count.
 * – Cais.
 *  Thread Starter [kimaldis](https://wordpress.org/support/users/kimaldis/)
 * (@kimaldis)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227860)
 * That’s useful. Thank you very much.
 * I don’t suppose you have any pointers towards getting going with custom lightboxes?
 * – K.
 *  Plugin Contributor [photocrati](https://wordpress.org/support/users/photocrati/)
 * (@photocrati)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227876)
 * [@kimaldis](https://wordpress.org/support/users/kimaldis/) – Our “custom” option
   for the Lightbox Effects is basically a fill in the blanks approach. If you want
   to “hard-code” the trigger for your custom Lightbox library you would need to
   also create a custom template … there are no parameters in any of our displays
   that can set a Lightbox at this time.
 * – Cais.
 *  Thread Starter [kimaldis](https://wordpress.org/support/users/kimaldis/)
 * (@kimaldis)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227909)
 * Looking at ng shortcode thing in gallery settings properly for the first time,
   it does seem like an odd way to deal with templates, doesn’t it? You’re effectively
   asking a user to select a gallery display type, which would show a gallery as
   a particular type, then select a template that will most probably show the gallery
   completely differently to the type selected. And no way to pass parameters to
   the template, other than those allowed in the gallery display settings. It’s 
   all very counter-intuitiveSurely a better way would be to allow a developer to
   add templates, along with parameters, to the display type list?
 * I mention this, partly because it’s so odd and partly because oddness seems to
   thread it’s way through Nextgen. From a standpoint, Nextgen is a bit of a mess.
   No offence intended.
 *  Thread Starter [kimaldis](https://wordpress.org/support/users/kimaldis/)
 * (@kimaldis)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227910)
 * From a _developers’_ standpoint. Sorry.
 *  Plugin Contributor [photocrati](https://wordpress.org/support/users/photocrati/)
 * (@photocrati)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227912)
 * [@kimaldis](https://wordpress.org/support/users/kimaldis/) – The template “system”
   is a NextGEN Legacy hold-over we maintain for backward compatibility, there is
   actually no real “template” system in the same sense with current NextGEN Gallery
   display types.
 * A developer should be easily able to create a “new” display type although it 
   is a bit involved, here is a link to a “draft” document that goes through the
   details: [https://0x18.us/creating-a-new-nextgen-display-type/](https://0x18.us/creating-a-new-nextgen-display-type/)
 * Perhaps this will be of better use … and thanks for the feedback, it is much 
   appreciated!
 * – Cais.
 *  Thread Starter [kimaldis](https://wordpress.org/support/users/kimaldis/)
 * (@kimaldis)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227914)
 * That’s an extremely useful link. Thanks.
 * I think the hardest thing in all of this is actually trying to find information
   like this. There should be a wiki or something. Or maybe there is?
 *  Plugin Contributor [photocrati](https://wordpress.org/support/users/photocrati/)
 * (@photocrati)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227920)
 * [@kimaldis](https://wordpress.org/support/users/kimaldis/) – At the moment, unfortunately
   not, but it is something that is on our internal roadmap to improve upon.
 * – Cais.
 *  Thread Starter [kimaldis](https://wordpress.org/support/users/kimaldis/)
 * (@kimaldis)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/#post-7227922)
 * Once again, thanks for this. I now have a much clearer idea of what’s going on.
 * I have one last question. Settings parameters defaults in display types, as described
   in the above link; Adding new parameters with defaults is just fine, so long 
   as I bump the module version, but changing a default – in mixin.wombat_display_type_mapper.
   php – has no effect on default values, even when the version is bumped. Any thoughts
   on what I might be missing?

Viewing 15 replies - 1 through 15 (of 18 total)

1 [2](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/page/2/?output_format=md)

The topic ‘Gallery template, access shortcode attributes’ is closed to new replies.

 * ![](https://ps.w.org/nextgen-gallery/assets/icon-256x256.png?rev=2083961)
 * [Photo Gallery, Sliders, Proofing and Themes - NextGEN Gallery](https://wordpress.org/plugins/nextgen-gallery/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/nextgen-gallery/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/nextgen-gallery/)
 * [Active Topics](https://wordpress.org/support/plugin/nextgen-gallery/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/nextgen-gallery/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/nextgen-gallery/reviews/)

## Tags

 * [attribute](https://wordpress.org/support/topic-tag/attribute/)
 * [nextgen](https://wordpress.org/support/topic-tag/nextgen/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)
 * [template](https://wordpress.org/support/topic-tag/template/)

 * 18 replies
 * 2 participants
 * Last reply from: [photocrati](https://wordpress.org/support/users/photocrati/)
 * Last activity: [10 years, 1 month ago](https://wordpress.org/support/topic/gallery-template-access-shortcode-attributes/page/2/#post-7227929)
 * Status: not a support question