Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save shansmith01/1360e94566e0cb4465a2a377b877016b to your computer and use it in GitHub Desktop.

Select an option

Save shansmith01/1360e94566e0cb4465a2a377b877016b to your computer and use it in GitHub Desktop.
Custom gallery format for WordPress (using Bootstrap v4 grid).

Custom gallery format (using Bootstrap v3 grid)

Usage

Place the custom-gallery-markup.php file wherever you like in your theme directory. For sake of this example it is inc directory.

In your functions.php file add those lines:

require_once('inc/custom-gallery-format.php'); // change the path to match the location if needed
add_filter('post_gallery', 'custom_gallery_grid', 10, 3);

Fixed/added 'missing' features

  1. Generating thumbnail size set by user via WordPress gallery panel.
  • Using full size image was waste of bandwidth.
  • If original images were not same size gallery might appear not fine.
  • The CMS user might be confused if he/she set specific thumbnail size and gallery would show different size/proportions.
  1. Generating thumbnail alt attribute set by user via WordPress gallery panel.
  2. Make thumbnails linking (set via WordPress gallery panel) work correctly (none, attachment page, file).

Minor changes

  • Renamed function bootstrap_gallery to custom_gallery_grid so anyone can adapt it to any other framework without the name beeing misleading.
  • Changed the name of $atts parameter to $attrs.
  • Removed redutant $key from foreach loop.
  • Changed the name of $value variable to $imageID in foreach loop.
  • Added wrapper section.gallery around all div.rows and removed .gallery class from each .rows.
  • Changed default column class from .col-md-X to .col-sm-X.
  • Added function custom_gallery_item for generating single gallery item.
  • Changed if-else to switch for columns + added 4 and 6 columns layout (5, 7, 8 and 9 fall back to 3).
  • Changed name of the variable $return to $gallery.
  • Changed single item from div to figure.
  • Changed size of displayed file to large insted of full.
  • Added ability to give thumbnail link more attributes.
  • Added comments.

TODO

  • Thumbnail captions with figcaption.
  • Gallery layout for 5, 7, 8 and 9 columns (maybe with .col-sm-offset-X?).
  • Title attribute for gallery links maybe?
@davidrslopes
Copy link

This is awesome, thank you very much Shannon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment