Swiper Gallery
Overview
A touch‑enabled image carousel powered by Swiper.js, with aspect ratio control, shade overlays, and a live editor preview.
- Block name:
method/swiper-gallery - Category:
method-component-blocks - Supports:
align(full) - Render type: Dynamic (PHP
render_callback) - Inner blocks: No
Usage
The Swiper Gallery block creates a responsive image slideshow with swipe navigation, pagination dots, and prev/next arrows. Images are selected through WordPress’s media gallery picker.
- Insert a Swiper Gallery block.
- Click Select Images in the Inspector to open the media gallery.
- Select multiple images and confirm.
- Optionally configure aspect ratio and shade overlay in the Inspector.
The editor renders a live Swiper instance that can be navigated with the arrow buttons. Swiper is initialized via useEffect with proper cleanup on image changes.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
useParallax | boolean | false | Reserved for future use |
bgColor | string | — | Background color |
bgGradient | string | — | Background gradient |
bgShadeColor | string | — | Per‑slide shade overlay color |
bgShadeGradient | string | — | Per‑slide shade overlay gradient |
images | array | [] | Array of image objects: { id, url, alt } |
responsiveSettings | object | (see below) | Spacing, border, and aspect ratio per breakpoint |
responsiveSettings Keys
aspectUses—"ratio"to use a preset ratio, empty for natural (fluid) image sizingaspectRatio— Preset string like"-16-9","-4-3","-1-1"
Control Sets
| Component | Panel | Breakpoints | Notes |
|---|---|---|---|
| Gallery MediaUpload | Gallery Settings | — | WordPress MediaUpload with multiple and gallery mode |
MethodAspectRatio | Image Settings | base | Preset ratio selector |
MethodColorControls | Colors | global | bgColor, bgShadeColor |
MethodStyleTag | — | — | Scoped editor styles |
Data & Dependencies
- Swiper.js:
swiper-bundle.min.jsandswiper-bundle.min.cssare enqueued on all pages viamethod_swiper_gallery_enqueue_assets. - Image sizes: The frontend renders images at the
method_hdregistered image size. - Editor initialization: Swiper is initialized in a
useEffectwith asetTimeout(0)to ensure DOM readiness. The instance is stored in a ref and properly destroyed on cleanup.
Frontend Rendering
<div class="method-swiper-gallery" id="method-{id}">
<div class="swiper">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="method-fit-img-container {method-ratio method-ratio-X-Y?}">
<div class="method-img-shade"> </div>
<img class="method-fit-img" src="..." alt="..." />
</div>
</div>
...
</div>
<div class="swiper-pagination"></div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const swiper = new Swiper('#method-{id} .swiper', {
loop: true,
slidesPerView: 1,
pagination: { el: '#method-{id} .swiper-pagination' },
navigation: {
nextEl: '#method-{id} .swiper-button-next',
prevEl: '#method-{id} .swiper-button-prev',
},
});
});
</script>When no aspect ratio is set (aspectUses is empty), images render with method-swiper-img-container and method-fluid-img classes for natural dimensions. When a ratio is set, images use method-fit-img-container with method-ratio classes.
Filters
This block does not currently expose dedicated PHP filters. Swiper configuration (loop, slidesPerView, etc.) is hardcoded in the inline <script> tag. To customize Swiper options, override the render callback or use JavaScript to reinitialize Swiper after DOMContentLoaded.