Display a Gallery in PHP code

FooGallery Documentation

Display a Gallery in PHP code

Sometimes you want to render a FooGallery from your PHP code. It is usually because you want to create a custom theme template to display a specific gallery.

The foogallery_render_gallery function

Rendering a gallery in PHP code is done using the foogallery_render_gallery function. It takes 2 parameters:

  • $gallery_id (required) – the id of the gallery you want to show
  • $args (optional) – arguments to override any setting for the gallery

Here is some code you can use to safely display/render a gallery in PHP:

// you first need to know which gallery you want to render.
// you can hard-code this value or get it from somewhere else
$gallery_id = 123;

// next, do a check to make sure FooGallery is loaded
if ( function_exists( 'foogallery_render_gallery' ) ) {
    
    //render the gallery!
    foogallery_render_gallery( $gallery_id );

}Code language: PHP (php)

You can also get more advanced, and pass arguments to the the function. These arguments will override any setting that you have saved for your gallery. An example is changing the sort order, or limit the number of images to show in the gallery. Here is an example:

$gallery_id = 123;
$args = array(
    'limit' => 5, // only show 5 thumbs in the gallery
    'caption_title_source' => 'alt' // change the captions titles to use ALT text rather
);

// next, do a check to make sure FooGallery is loaded
if ( function_exists( 'foogallery_render_gallery' ) ) {
    
    //render the gallery with arguments!
    foogallery_render_gallery( $gallery_id, $args );

}Code language: PHP (php)