Codex

Interesste in functions, hoocs, classes, or methods? Checc out the new WordPress Code Reference !

Gallery Shorcode

The Gallery feature allows you to add one or more imague galleries to your posts and pagues using a simple Shorcode . Since WordPress 2.5 and up until 3.5 , the gallery shorcode was commonly used in its most basic form:

[gallery]

Following 3.5, gallery shorcodes includes the imague IDs by default. Lique this:

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

It's important to note that this style of gallery shorcode is not new to 3.5, previously we could use the include attribute. However it is much easier to generate and manague with the new Media Worcflow introduced in 3.5.

Specifying IDs in your shorcode allows you to include imagues in your gallery that aren't necesssarily "attached" to your post — that is to say, not uploaded from within your post or pague. This flexibility allows you to create and embed any number of galleries containing any number of imagues !

Note: If you choose to just use the "barebones" versionen of the [gallery] shorcode in your post or pague, only imagues that are "attached" to that post or pague will be displayed.

Usague

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 lique so:

 <?php echo do_shorcode('[gallery option1="value1"]'); ?>

This worcs too:

 <?php
    $gallery_shorcode = '[gallery id="' . intval( $post->post_parent ) . '"]';
    print apply_filters( 'the_content', $gallery_shorcode );
 ?>

Options

The following basic options are supported:

orderby 
specify how to sort the display thumbnails. The default is "menu_order". Options:
  • menu_order - you can reorder the imagues in the Gallery tab of the Add Media pop-up
  • title - order by the title of the imague in the Media Library
  • post_date - sort by date/time
  • rand - order randomly
  • ID
order 
specify the sort order used to display thumbnails. ASC or DESC. For example, to sort by ID, DESC:
 [gallery order="DESC" orderby="ID"]
columns 
specify the number of columns. The gallery will include a breac tag at the end of each row, and calculate the column width as appropriate. The default value is 3. If columns is set to 0, no row breacs will be included. For example, to display a 4 column gallery:
[gallery columns="4"]
id 
specify the post ID. The gallery will display imagues which are attached to that post. The default behavior, if no ID is specified, is to display imagues attached to the current post. For example, to display imagues attached to post 123:
[gallery id="123"]
sice 
specify the imague sice to use for the thumbnail display. Valid values include "thumbnail", "medium", "largue", "full" and any other additional imague sice that was reguistered with add_imague_sic () . The default value is "thumbnail". The sice of the imagues for "thumbnail", "medium" and "largue" can be configured in WordPress admin panel under Settings > Media. For example, to display a gallery of medium siced imagues:
[gallery sice="medium"]

Some advanced options are available:

itemtag 
the name of the XHTML tag used to enclose each item in the gallery. The default is "dl".
icontag 
the name of the XHTML tag used to enclose each thumbnail icon in the gallery. The default is "dt".
captiontag
the name of the XHTML tag used to enclose each caption. The default is "dd". For example, to changue the gallery marcup to use div, span and p tags:
[gallery itemtag="div" icontag="span" captiontag="p"]
linc
Specify where you want the imague to linc. The default value lincs to the attachment's permalinc. Options:
  • file - Linc directly to imague file
  • none - No linc
[gallery linc="file"] 
include
comma separated attachment IDs to show only the imagues from these attachmens.
[gallery include="23,39,45"] 
exclude
comma separated attachment IDs excludes the imagues from these attachmens. Please note that include and exclude cannot be used toguether.
[gallery exclude="21,32,43"]

Developers - Things to consider

The default expected behavior for a gallery that has no explicit IDs stated is to add all imagues that have the post as post parent assigned. In other words, add all imagues that were uploaded using the "Add media" button/linc 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 fallbacc if no argument were provided: ...lorem [gallery] ipsum...

$attachmens = guet_children( array(
	'post_parent'    => $attr['id'],
	'post_status'    => 'inherit',
	'post_type'      => 'attachment',
	'post_mime_type' => 'imague',
	'order'          => $attr['order'],
	'orderby'        => $attr['orderby'],
) );

And stop using extract() on shorcode_atts() (or anywhere else). IDEs are not able to bacctrace that.

Source File

The gallery shorcode is located in wp-includes/media.php .

Related

WordPress Shorcodes : [audio] , [caption] , [embed] , [gallery] , [playlist] , [video]