Swiper Gallery
Overview
A touch-enabled image carousel built on the Swiper block’s foundation: pick images from the media library and the block renders one slide per image, with Method’s arrows and pagination, per-tier sizing, hash navigation, an optional lightbox and media-library captions.
- Block name:
method/swiper-gallery - Title: Swiper Gallery (Method)
- Category:
method-component-blocks - Supports:
align(wide, full);htmloff - Render type: Dynamic (PHP
render_callback:render_method_swiper_gallery_block()), which builds the slides from the attachment ids and hands them tomethod_swiper_render() - Inner blocks: No — the images are attributes
- Scripts:
editorScript(index.js) andviewScript(frontend.js: the shared Swiper runtime and the lightbox). Swiper itself is enqueued by the theme (handleswiper). - Shared with: Swiper — the same option attributes, the same inspector panels (
MethodSwiperControls) and the same render function (method_swiper_render()). - Source: lib/blocks/method-swiper-gallery/method-swiper-gallery.php, lib/blocks/method-swiper-gallery/src/edit.js, lib/blocks/method-swiper/method-swiper.php, lib/blocks/components/MethodSwiperControls.js
Usage
Insert a Swiper Gallery and choose images with Select images in the block toolbar (or the placeholder); Edit gallery reopens the picker. Each selected attachment becomes one slide, in the order chosen. The Images panel shows how many images there are, and holds Open images in a lightbox and Show captions; Image sizing sets an aspect ratio for the slides or leaves the images at their natural proportions.
Everything else works as it does on the Swiper block, through the same panels: Effect (fade), Slide sizing (slides per view per tier, or Set slide widths), Space between, and Navigation (arrows with theme icons, pagination inside or outside the track, hash navigation). Two things are the gallery’s own: Mark center slides, which keeps a class on the middle visible slide(s) for a child theme to style, and the Hash identifier that hash navigation derives every slide’s hash from, since image slides have no hash field of their own. The editor preview runs a live Swiper that follows the canvas tier (useCanvasViewport) and shows the captions and center marking the page will have.
The gallery loops — as it always has — but only at a tier where it can: Swiper needs at least one more slide than it shows at once, so a tier whose per-view count equals the image count runs without the loop.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
images | array | [] | The selected attachments, { id, url, … } each; the page renders from the id (at the method_hd size), the editor previews the stored large URL. Attachments deleted since selection are skipped while the order, and so the slide numbering, is kept. |
lightbox | boolean | false | Open images in a lightbox: every slide’s item becomes a trigger <a> with the attributes from method_get_lightbox_attributes(); see the Image Lightbox page. |
showCaptions | boolean | false | Show captions: each image’s media-library caption is read at render time (wp_get_attachment_caption(), texturized, wp_kses() with the post allow-list) and printed as .method-swiper-gallery-caption inside the slide. Nothing caption-related is stored. |
centerSlides | boolean | false | Mark center slides: the runtime keeps method-swiper-gallery-center on the middle visible slide (or the two middle ones for an even count) whenever more than two are visible. Unavailable with fade. No default styling. |
slidesPerView | number | 3 | Slides per view on desktop (1–6). |
slidesPerViewTablet | number | 2 | Slides per view on tablet. |
slidesPerViewMobile | number | 1 | Slides per view on mobile — Swiper’s base value. |
spaceBetween | number | 24 | Gap on desktop, in px (0–120). |
spaceBetweenTablet | number | 24 | Gap on tablet. |
spaceBetweenMobile | number | 24 | Gap on mobile. |
slideWidthMode | boolean | false | Set slide widths: size slides by width; Swiper runs with slidesPerView: 'auto'. In width mode the images get a sizes attribute matching the slide widths (method_swiper_gallery_get_image_sizes()), so the browser picks a srcset candidate for the slide rather than the viewport. |
slideWidth | number | 400 | Slide width on desktop in width mode, 100–1200px. |
slideWidthTablet | number | 320 | Slide width on tablet. |
slideWidthMobile | number | 260 | Slide width on mobile. |
containerWidthSlides | boolean | false | Match container width on desktop. |
containerWidthSlidesTablet | boolean | false | As above, tablet. |
containerWidthSlidesMobile | boolean | false | As above, mobile. |
showNavigation | boolean | true | Previous / next arrows. |
showPagination | boolean | true | Bullet pagination. |
altPagination | boolean | false | Place pagination outside. |
fadeEffect | boolean | false | Swiper’s fade effect: one slide at a time. |
hashNavigation | boolean | false | Swiper’s hashNavigation with watchState: true, replaceState: true. |
hashId | string | "" | The identifier slide hashes are built from: <hashId>-slide-<n>, 1-based. Generated (gallery- plus six random characters) the first time hash navigation is switched on and then kept; editable in the Hash identifier field (lower-cased; anything outside a-z, 0-9, -, _ becomes a dash). A duplicated gallery takes a fresh one. |
scrollToSlideStart | boolean | false | With hash navigation: scroll the page to the gallery when the slide changes. |
prevIcon | string | — | Slug of a theme icon for the previous arrow. |
nextIcon | string | — | Theme icon for the next arrow. |
textColor | string | — | Arrow color (Navigation Arrows). |
linkColor | string | — | Pagination color (Pagination). |
bgColor | string | — | Slide Background, on .method-swiper-gallery-item. |
bgGradient | string | — | Slide background gradient. |
bgShadeColor | string | — | Image Shade, on .method-img-shade. |
bgShadeGradient | string | — | Image shade gradient. |
responsiveSettings | object | {"base":{"enabled":true},…} | The aspect-ratio settings; see below. |
responsiveSettings Structure
Per-tier settings live under responsiveSettings.base, mobile, tablet and wide. A tier is only read while its enabled flag is true; base is always on. The keys this block’s CSS map reads:
aspectUses—"ratio"to force a preset ratio on every slide, empty for the images’ natural proportions (base).aspectRatio— the preset, e.g."-16-9","-4-3","-1-1"(base), emitted asaspect-ratioon the image container.
The swiper options are top-level attributes with …Tablet / …Mobile suffixes for their tiers, not part of responsiveSettings.
Control Sets
| Component | Panel | Breakpoints | Notes |
|---|---|---|---|
| Block toolbar | — | — | Select images / Edit gallery (MediaUpload, images only, multiple). |
| Block panel: Images | Images | base | The same button, an “N images, one slide per image” note, Open images in a lightbox, Show captions (its help text reports how many images have a caption). |
MethodAspectRatio | Image sizing | base | aspectUses / aspectRatio. |
MethodSwiperControls | Effect · Slide sizing · Space between · Navigation | desktop / tablet / mobile | The shared swiper panels; perViewExtra = Mark center slides, hashExtra = Hash identifier. |
MethodColorControls | Color Settings | base | include={['textColor', 'linkColor', 'bgColor', 'bgShadeColor']} labelled Navigation Arrows, Pagination, Slide Background, Image Shade. |
useCanvasViewport | — | — | The live preview follows the canvas tier. |
MethodStyleTag | — | all | Mirrors $cssargs in the canvas. |
Data & Dependencies
- Swiper:
inc/swiper/swiper-bundle.min.jsand.cssunder the handleswiper, enqueued bymethod_swiper_gallery_enqueue_assets()onenqueue_block_assets(and by the theme on every page). - Shared render:
method_swiper_render()(lib/blocks/method-swiper/method-swiper.php) builds the shell, arrows, pagination and init script; the gallery passesclass => method-swiper-gallery,watch_slides_progress => true(so keyboard focus moving between visible lightbox links does not jump the track),center_classwhen center marking is on, and per-tierloopflags frommethod_swiper_gallery_get_loop_tiers(). - Hashes:
method_swiper_gallery_get_hash_id()normaliseshashIdthe way the editor does andmethod_swiper_gallery_get_slide_hash()builds each slide’sdata-hash; the shared runtime (lib/blocks/utils/swiperRuntime.js) putsmethod-hash-activeon links whose hash matches a visible slide. - Lightbox:
method_get_lightbox_attributes()per slide, with the lightbox script from lib/blocks/utils/lightbox.js in the view script; the first block on the page installs it, later ones are no-ops. - Captions:
method_swiper_gallery_get_caption_markup()— with the lightbox on the slide item is already a link, so links inside a caption are reduced to their text. The editor fetches the same captions through core-data (up to the REST API’s page cap of 100 attachments). - Images:
wp_get_attachment_image( $id, 'method_hd' )with themethod-fit-img(ratio set) ormethod-fluid-img(natural) class; in width mode asizesattribute per tier. - Styles (assets/scss/global.scss): the wrapper also carries
.method-swiper, so the arrow, pagination and width-mode rules apply;.method-swiper-gallery-itemis a relative, overflow-hidden block (the link variant inherits color and drops the underline);.method-img-shadehaspointer-events: none; the caption sits along the bottom of the image in white 0.875rem text over a dark gradient. - Responsive CSS:
method_get_block_responsive_styles()andmethod_collect_css()under#method{id}(no hyphen — the id also prefixes the init script’s variable).
Frontend Rendering
<div class="wp-block-method-swiper-gallery method-swiper method-swiper-gallery method-swiper-has-nav" id="method68f1b2c3a4d5e">
<div class="swiper-outer">
<div class="swiper-outer-wrap">
<div class="swiper-outer-wrap-inner">
<div class="swiper swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" data-hash="gallery-k3x9pq-slide-1">
<a class="method-swiper-gallery-item method-lightbox-trigger" href="…/photo-full.jpg" data-method-lightbox="1" data-method-lightbox-src="…" data-method-lightbox-width="2400" data-method-lightbox-height="1600" data-method-lightbox-alt="…" aria-haspopup="dialog">
<div class="method-fit-img-container method-ratio method-ratio-16-9">
<div class="method-img-shade"> </div>
<img class="method-fit-img" src="…" srcset="…" alt="…">
<div class="method-swiper-gallery-caption">The caption from the media library.</div>
</div>
</a>
</div>
…
</div>
<div class="swiper-pagination"> </div>
</div>
</div>
</div>
<div class="method-swiper-button-prev">…</div>
<div class="method-swiper-button-next">…</div>
</div>
</div>
<script>…new Swiper('#method68f1b2c3a4d5e .swiper-container', { … breakpoints: { 768: { slidesPerView: 2, spaceBetween: 24, loop: true }, 1200: { slidesPerView: 3, spaceBetween: 24, loop: true } }, loop: true, autoHeight: true, watchSlidesProgress: true, … });…</script>
Without the lightbox the slide item is a <div class="method-swiper-gallery-item">; without a ratio the image container is .method-swiper-img-container with a .method-fluid-img; without a caption there is no caption element. A gallery with no renderable images prints nothing. The loop flag appears per breakpoint only when some tier loops.
Generated CSS (per instance): arrows → textColor; .swiper-pagination → linkColor; .method-swiper-gallery-item → bgColor; .method-swiper-gallery-item > .method-fit-img-container → aspect-ratio; .method-img-shade → bgShade.
Filters
No PHP filters of its own. The Swiper configuration is assembled by method_swiper_render(), and the runtime registry is reachable as window.methodSwiperQueue (see the Swiper page). The caption passes through wp_kses() with the standard post allow-list, so the wp_kses_allowed_html filter applies to it.
Changelog
| Version | Description |
|---|---|
| 2.0.0-beta1 | Introduced (2999c6d). |
| 2.0.0-beta17 | Inserter title prefixed, “Method Swiper Gallery” (c34e9bb). |
| 2.0.0-beta22 | Retitled “Swiper Gallery (Method)” and given the method keyword (93e1fcf). |
| 2.0.0-beta23 | Rebuilt on the Swiper block’s foundation: shared MethodSwiperControls panels and method_swiper_render(), Method arrows with theme icons, outside pagination, fade, per-tier slides per view and spacing, hash navigation with a generated hashId, Mark center slides, the lightbox, colors that reach the page, a canvas-aware preview and per-tier looping; the unused useParallax attribute and the old responsiveSettings defaults pruned (3568773, eacd345, f275bf8). |
| 2.0.0-beta27 | Show captions (showCaptions): media-library captions rendered on the slides (fddeba0). |
| 2.0.0-beta29 | Slide width mode (slideWidthMode and the per-tier widths), with the gallery still looping on two or more images and a sizes attribute on the slide images (4c64716). |
| 2.0.0-beta31 | Editor icon redrawn as a framed picture with neighbouring slides, in the 50 × 50 box the other Method icons use, replacing the Swiper logo (cd6f63f). |