Hi, I have a collection of custom posts each post has 0 - 6 images which I'd like to display in a Bootstrap carousel (one at a time, with <, > arrows). Think I need to use "wpv-for-each" loop but not sure how, appreciate a code snippet to see an example of how to do this.
This is what I have - copy/pasted from a bootstrap eg.
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img src="//placehold.it/640x480" class="img-responsive" >
</div>
<div class="carousel-item">
<img class="d-block w-100" src="//placehold.it/800x400/ff9203" alt="Second slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
Thanks
David
Hi David,
Thank you for contacting us and I'd be happy to assist.
For this example, let's assume that you're using an image type custom field with slug "book-images" and the field is set to have multiple instances.
In that case, the code for the carousel will use the images from the field like this:
<div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
[wpv-for-each field="wpcf-book-images" start="1" end="1"]
<div class="carousel-item active">
<img src="[types field='book-images' output='raw'][/types]" class="d-block w-100" alt="...">
</div>
[/wpv-for-each]
[wpv-for-each field="wpcf-book-images" start="2"]
<div class="carousel-item">
<img src="[types field='book-images' output='raw'][/types]" class="d-block w-100" alt="...">
</div>
[/wpv-for-each]
</div>
<a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
Notes:
1. To get the image URL of each instance of the field, "Types Fields API" shortcode is used with output='raw':
https://toolset.com/documentation/customizing-sites-using-php/functions/#image
2. To loop through the available multiple instances of the fields, the repeating part of the carousal code is wrapped inside the "wpv-for-each" shortcode:
https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-for-each
3. We had to use two different instances of the "wpv-for-each" shortcode since the carousel structure expects the first element to have the "active" class.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Thanks Waqar,
Looks like exactly what I need. Will try it later and let you know.
David
You're very welcome David.
Please let me know how it goes and if you have any follow-up questions.
For a new question or concern, please start a new ticket.
Thanks Waqar, Your solution worked perfectly out of the box.