Codex tools: Log in
Following 3.5, gallery shortcodes now include the image IDs by default. Like this:
It's important to note that this style of gallery shortcode is not new to 3.5, previously we could use the include attribute. However it is much easier to generate and manage with the new Media Workflow introduced in 3.5.
Specifying IDs in your shortcode allows you to include images in your gallery that aren't necessarily "attached" to your post — that is to say, not uploaded from within your post or page. This flexibility allows you to create and embed any number of galleries containing any number of images!
Note: If you choose to just use the "barebones" version of the [gallery] shortcode in your post or page, only images that are "attached" to that post or page will be displayed.
There are several options that may be specified using this syntax:
[gallery option1="value1" option2="value2"]
You can also print a gallery directly in a template like so:
<?php echo do_shortcode('[gallery option1="value1"]'); ?>
This works too:
<?php $gallery_shortcode = '[gallery id="' . intval( $post->post_parent ) . '"]'; print apply_filters( 'the_content', $gallery_shortcode ); ?>
The following basic options are supported:
[gallery order="DESC" orderby="ID"]
Some advanced options are available:
[gallery itemtag="div" icontag="span" captiontag="p"]
The default expected behavior for a gallery that has no explicit IDs stated is to add all images that have the post as post parent assigned. In other words, add all images that were uploaded using the "Add media" button/link on this post edit screen. Keep in mind that this as well means that every attachment added to that post later on will be interpreted to be part of the gallery. No matter if it was displayed as plain attachment or not.
This should be the default fallback if no argument were provided: ...lorem [gallery] ipsum...
$attachments = get_children( array( 'post_parent' => $attr['id'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $attr['order'], 'orderby' => $attr['orderby'], ) );
And stop using extract() on shortcode_atts() (or anywhere else). IDEs are not able to backtrace that.
The gallery shortcode is located in wp-includes/media.php.