Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Image Gallery Short Code Using Post Attachments

Posted on December 12, 2011 by Pippin in Intermediate, Quick Tips, Short Codes, Tutorials, Working with Attachments 18 Comments
Home» Tutorials » Intermediate » Image Gallery Short Code Using Post Attachments
Tweet
Love It - 1

The built-in WordPress gallery systems leaves much to be desired. It works well if a theme supports it, but it can be difficult to customize, especially if you have any special markup you’d like with the gallery. While working on a client’s theme, I wrote up a quick short code function that can be used to display a gallery of all the attached images in any way that you like. The function is quite simple, and will be much easier to modify (especially in terms of markup) than the built-in gallery system.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function pippin_gallery_shortcode( $atts, $content = null ) {
 
	extract( shortcode_atts( array(
      'size' => ''
      ), $atts ) );
 
	$image_size = 'medium';
	if($size =! '') { $image_size = $size; }
 
	$images = get_children(array(
		'post_parent' => get_the_ID(),
		'post_type' => 'attachment',
		'numberposts' => -1,
		'post_mime_type' => 'image',
		)
	);
 
	if($images) {
		$gallery = '<ul class="gallery clearfix">';
		foreach( $images as $image ) {
			$gallery .= '<li>';
				$gallery .= '<a href="' . wp_get_attachment_url($image->ID) . '" rel="shadowbox">';
					$gallery .= wp_get_attachment_image($image->ID, $image_size);
				$gallery .= '</a>';
			$gallery .= '</li>';
		}
		$gallery .= '</ul>';
 
		return $gallery;
	}
 
}
add_shortcode('photo_gallery', 'pippin_gallery_shortcode');

All this function does is get the ID of the current post, then check to see if it has any image attachments. If image attachments are present, then the images are looped through and displayed one at a time in an unordered list. Each image is linked to its own file with a rel=”shadowbox” for a lightbox effect (if you have the JS loaded).

The gallery can then be shown for a post or page by using the photo_gallery short code, like so:

[photo_gallery]

Since the function is just a simple loop, you can also customize the HTML markup exactly as you want.


Related Items
  • Plugin Development 101 – Intro to Short Codes
Tweet Follow @pippinsplugins
attachments, images, short code

18 comments on “Image Gallery Short Code Using Post Attachments”

  1. Bruce says:
    December 12, 2011 at 8:12 am

    Excellent tip. Never thought of this… :)

    Reply
    • Pippin says:
      December 12, 2011 at 8:52 am

      Glad you like it!

  2. Blaine says:
    December 12, 2011 at 9:12 am

    It’s worth noting that this will also display the featured image if present. If you’d like to exclude this, pass in the following argument:

    ‘exclude’ => get_post_thumbnail_id()

    I had an instance where a client wanted the ability to order the gallery manually. This can be achieved by utilizing WordPress’ built-in ‘Menu order’ option within the gallery’s settings. Just pass the following in as well.

    ‘orderby’ => ‘menu_order’

    Reply
    • Pippin says:
      December 14, 2011 at 1:55 pm

      @Blaine – Thanks for the note. That is important to know.

  3. GreggFranklin says:
    December 21, 2011 at 12:58 pm

    Thanks Pippin, Just what I was looking for.

    Reply
    • Pippin says:
      December 21, 2011 at 1:02 pm

      Excellent, glad to help.

  4. Claudio says:
    February 7, 2012 at 1:30 pm

    Great!! is there any chance to exclude always the first item in the gallery?? or hide it from the gallery grid and show it in another place in the page?? Thanks in advance.

    Reply
    • Pippin says:
      February 7, 2012 at 1:44 pm

      Why would you want to exclude the first item? Are you thinking of something along the lines of excluding the featured image?

  5. Claudio says:
    February 7, 2012 at 3:56 pm

    What I’m trying to do is a portfolio page and upload a bunch o images hide the first image which will be a logo and then put it in another place in the portfolio page. Hope you understand.

    Reply
    • Pippin says:
      February 7, 2012 at 6:30 pm

      I would personally set the logo as the Featured Image. If you can do that, then I can show you how to exclude that image.

  6. tommysvr says:
    March 31, 2012 at 6:41 pm

    Hi Pippin,

    Is there anyway you could give the first image a different class? I’m looking to use this in a slider and the first one needs a class of ‘active’.

    Also it doesn’t seem to like my sizing and always ends up making it a thumbnail:

    extract( shortcode_atts( array(
    'size' => ''
    ), $atts ) );

    $image_size = 'medium';
    if($size =! '') { $image_size = $size; }

    $images = get_children(array(
    'post_parent' => get_the_ID(),
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_mime_type' => 'image',
    'orderby' => 'menu_order',
    'exclude' => get_post_thumbnail_id()
    )
    );

    if($images) {
    $gallery = '';
    foreach( $images as $image ) {
    $gallery .= '';
    $gallery .= wp_get_attachment_image($image->ID, $image_size);
    $gallery .= '';
    }
    $gallery .= '';
    $gallery .= '‹';
    $gallery .= '›';

    return $gallery;
    }

    Reply
    • Pippin says:
      April 1, 2012 at 10:28 am

      For adding a class to the foreach, you can do it like this:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      
      $i = 0;
      foreach( $images as $image ) {
      	if($i == 0) {
      		$class = 'first-image-class';
      	} else {
      		$class= 'other-image-class';
      	}
      	$gallery .= '<li class="' . $class . '">';
      		$gallery .= wp_get_attachment_image($image->ID, $image_size);
      	$gallery .= '</li>';
      	$i++;
      }

      For the sizes, what size are you trying to use? Is it a custom size? If it is custom, then make sure you have regenerated the thumbnails.

  7. Peter says:
    January 30, 2013 at 1:41 pm

    Hello Pippin. I’d greatly appreciate it if you showed how to exclude the Featured image :) Also, have any ideas on how to maybe change the order of the images in the gallery once they’re added? Thanks for your time man and for sharing the code

    Reply
    • Peter says:
      January 30, 2013 at 1:53 pm

      Ok, so apparently I’m a dummy who should learn to read the whole thread before asking. I figured out how to do the exclude. How do I change the order though? Where and how do I modify this?

  8. Peter says:
    January 30, 2013 at 2:49 pm

    How do I order the gallery manually after passing the ‘orderby’ => ‘menu_order’ parameter? Please help a newbie out.

    Reply
    • Pippin says:
      January 31, 2013 at 9:08 am

      Use this plugin: http://wordpress.org/extend/plugins/post-types-order/

    • Peter says:
      February 2, 2013 at 12:07 pm

      Hello again Pippin. Thanks for taking the time to assist. I did not use the plugin you mentioned but managed to find another solution on my own and since it might help someone in the future I decided to share it here.

      The simple trick is that when editing a page use the Upload/insert button to bring up the Add Media dialogue. Then switch to the Gallery (x) tab which should be there. It lists all media attached to the given page. Then you can use the input fields on the right side to set index numbers for individual files. In my case I had to set them in a reverse order but it might have been my tinkering with your code that caused that. Anyways that’s it!

      Feel free to delete my multiple posts above to leave only what’s relevant. Take care man

    • Pippin says:
      February 4, 2013 at 11:32 am

      Great!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Simple Google Maps Short Code
  • Stripe Integration Part 3 – Variable Prices and Enhanced Plan Handling
  • User Submitted Image Gallery – Final Overview
  • User Submitted Image Gallery – Part 8
  • Adding a Simple Image Gallery with the Repeatable Upload Field

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes

Latest Tutorials

  • Storing Session Data in WordPress without $_SESSION (19)

    The term Session in web development refers to...

  • Test Your Plugins with RTL (1)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

Enter your email to receive automated updates when new posts are published

WP Core Contributions

  • [24316]

View the ticket on Trac.

WP Codex Contributions

  • Function: shortcode exists
  • Function: has shortcode
  • Function: shortcode exists
  • Function: shortcode exists
  • Function: has shortcode

View all 41 changes in the Codex.

Latest Tweets

  • Could not fetch Twitter RSS feed.

Topics

meta box the_content Tom McFarlin register_setting wp_enqueue_script attachments shortcodes contextual help add_options_page hook featured Sugar Event Calendar get_user_meta attachment image forms do_action plugin mail chimp login short codes authors Related posts recent posts post types apply_filters comments short code bbpress taxonomies custom post type images Ajax gallery Stripe taxonomy jquery widgets users add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins