Skip Navigation

[Resolved] set up bootstrap carousel for 0 – 6 images using wpv-for-each loop

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by davidm-13 4 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#1449685

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

#1450565

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

#1450809

Thanks Waqar,
Looks like exactly what I need. Will try it later and let you know.

David

#1450951

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.

#1453461

Thanks Waqar, Your solution worked perfectly out of the box.