Function and usage of gallery_shortcode() in wordpress

Question

Answers ( 1 )

    0
    2024-01-06T10:07:27+00:00

    The gallery_shortcode() function in WordPress is used for building the output of the Gallery Shortcode, which is a feature for displaying images in a post. Here's a detailed explanation of its functionality and parameters:

    Function:

    gallery_shortcode( array $attr ): string

    Purpose:

    • Builds the Gallery shortcode output.
    • This function implements the functionality of the Gallery Shortcode, enabling the display of WordPress images on a post in a gallery format.

    Parameters:

    • $attr (array): This is a required parameter. It contains the attributes of the gallery shortcode, which define how the gallery will be displayed. The attributes include:

      • order (string): Defines the order of the images in the gallery. Defaults to 'ASC' (Ascending). It accepts 'ASC' or 'DESC'.

      • orderby (string): Specifies the field to use when ordering the images. Defaults to ‘menu_order ID’. It accepts any valid SQL ORDERBY statement.

      • id (int): The Post ID to which the gallery is attached.

      • itemtag (string): HTML tag used for each image in the gallery. Defaults to 'dl', or 'figure' if the theme supports HTML5 gallery.

      • icontag (string): HTML tag for each image's icon. Defaults to 'dt', or 'div' for HTML5 gallery support.

      • captiontag (string): HTML tag for each image’s caption. Defaults to 'dd', or 'figcaption' for HTML5 gallery support.

      • columns (int): Number of columns of images to display. Defaults to 3.

      • size (string|int[]): Size of the images. Accepts any registered image size name, or an array of width and height values in pixels. Defaults to 'thumbnail'.

      • ids (string): A comma-separated list of attachment IDs to display. Defaults to empty.

      • include (string): A comma-separated list of attachment IDs to include. Defaults to empty.

      • exclude (string): A comma-separated list of attachment IDs to exclude. Defaults to empty.

      • link (string): Defines what each image links to. Defaults to empty (links to the attachment page). Accepts 'file', 'none'.

    Return:

    • string: The function returns HTML content that can be used to display the gallery.

    Sample Usage:

    // Example of using gallery_shortcode function
    $gallery_attributes = array(
        'order'      => 'ASC',
        'orderby'    => 'menu_order ID',
        'id'         => $post->ID,
        'itemtag'    => 'figure',
        'icontag'    => 'div',
        'captiontag' => 'figcaption',
        'columns'    => 4,
        'size'       => 'medium',
        'include'    => '123,234,345', // IDs of the images to include
        'link'       => 'file'
    );
    
    echo gallery_shortcode($gallery_attributes);
    

    In this sample, the gallery_shortcode function is called with an array of attributes to customize the gallery's appearance and behavior. The gallery will display images with IDs 123, 234, and 345 in a 4-column layout, each image linking to its file.

Leave an answer