Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

User:DrewAPicture/gallery shortcode

Gallery Shortcode

The Gallery feature allows you add to one or more image galleries to your posts and pages. Up until WordPress 3.5, the gallery shortcode was commonly used in its most basic form:

[gallery]

Following 3.5, gallery shortcodes now include the image IDs by default. Like this:

[gallery ids="729,732,731,720"]

It's important to note that this style of gallery shortcode is not new to 3.5, 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.

Output Options

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 );
 ?>