Home › Forums › ACF PRO › Multiple ACF Galleries via Shortcode › Reply To: Multiple ACF Galleries via Shortcode
you can do this with repeater
it works like this:
1. create a repeater field and assign to pages, has the name of image_gallery_components
2. repeater has : image_gallery_shortcode_name, gallery_of_images (the gallery)
3. using flexslider to display the gallery
4. person knows how to use shortcodes
I have done it so that each shortcode generates an indivdual instance of flexslider using some bang-hash-rush code, change it to work for you if you want.
The idea is to get the id from the shortcode, repeat through the rows of the repeater, and if the id in the short code is the same as the id of the repeater row, output the html for the gallery.
simple really. And remember you can do this with vimeo and youtube embeds as well.
in your functions.php
/**** begin custom gallery shortcode ******/
function galleryShortcode($atts) {
extract(shortcode_atts(array(
"id" => ''
), $atts));
$wrapperString ="";
$LiString = "";
$rand = rand(5, 3000);
if ( have_rows('image_gallery_components')){
$increment = 0;
while( have_rows('image_gallery_components') ): the_row();
if(get_sub_field("image_gallery_shortcode_name")==$id)
{
$galleryImages = get_sub_field("gallery_of_images");
foreach($galleryImages as $galleryImage)
{
$LiString .= "<li><img src='".$galleryImage['sizes']['large']."'></li>";
$increment+=1;
}
$wrapperString = "<div class='galleryWrap'>
<div id='slides".$rand."' class='flexslider slider'>
<ul class='slides'>".$LiString."</ul>
</div>";
if($increment>1)
{
$wrapperString.="<div id='carousel".$rand."' class='flexslider carousel'>
<ul class='slides'>".$LiString."
</ul>
</div>";
}
$wrapperString.="</div>";
}
endwhile;
if($increment>1)
{
$append = "
<script>
$(document).ready(function() {
// The slider being synced must be initialized first
$('#carousel".$rand."').flexslider({
animation: 'slide',
controlNav: false,
animationLoop: false,
slideshow: false,
itemWidth: 180,
itemMargin: 5,
asNavFor: '#slides".$rand."'
});
$('#slides".$rand."').flexslider({
animation: 'slide',
controlNav: false,
animationLoop: false,
slideshow: false,
sync: '#carousel".$rand."'
});
});</script>";
}
else
{
$append = "
<script>
$(document).ready(function() {
// The slider being synced must be initialized first
$('#slides".$rand."').flexslider({
animation: 'slide',
controlNav: false,
animationLoop: false,
slideshow: false,
});
});</script>";
}
}
$wrapperString.=$append;
return $wrapperString;
}
add_shortcode("customimagegallery", "galleryShortcode");
/****** end custom gallery shortcodes *********/
in your page or post (if you have assigned the location to it.
[customimagegallery id=”my-gallery”]
// content
[customimagegallery id=”second-gallery”]
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.