I'm using Wordpress and ACF flexible-layouts to create Flickity image galleries.
I have 2 issues:
- How do i count the number of slides in the gallery. ( I want to only run Flickity if there are greater than 2 slides )
- How to run multiple galleries on the same post. ( each have the same class name )
I would like to use the nuSkool Vanilla JS option for this. See docs: http://flickity.metafizzy.co/#initialize-with-vanilla-javascript
PHP
<div class="gallery-slider flickity">
<?php $i = 0; if(get_sub_field('gallery')): while(has_sub_field('gallery')): ?>
<?php $attachment = get_sub_field('images'); ?>
<img class="slide" src="<?php echo $attachment['sizes']['800cropped']; ?>" alt="<?php echo $attachment['alt']; ?>">
<?php $i++; endwhile; endif; ?>
</div>
Vanilla JS
'use strict'
const Flickity = require('flickity')
const galleries = document.querySelector('.gallery-slider')
if (galleries) {
const gallery = new Flickity( galleries, {
setGallerySize: true,
wrapAround: true,
pageDots: true,
prevNextButtons: true,
autoPlay: 10000,
imagesLoaded: true,
}
)}
Current status, the code is working with one gallery per post and has pageDots and prevNextButtons controls even if there is just one slide.
Any help would be great, thanks in advance :)
No jQuery responses please.