Home › Forums › General Issues › Bootstrap Carousel with ACF › Reply To: Bootstrap Carousel with ACF
This is more of a general coding question than an ACF specific question. Here are some of the problems that I see though.
The first thing is that you’re not doing a query before the indicators to know how many items you have. Maybe you had one and removed it to hard code them.
The second thing that I’ll tell you is that you should not be doing multiple queries to do the work, this will significantly slow down the page load.
I’m not extremely familiar with bootstrap carousels but the HTML code does not look completely correct. When I look at an example I found here http://www.w3schools.com/bootstrap/bootstrap_carousel.asp. I couldn’t find and example that looked like your code so you might want to take a look that the example again and make sure you have that right. Using the example that I gave above it should look something like this
<?php
$args = array (
'post_type' => 'testimonials',
'posts_per_page' => 1
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
?>
<div id="carousel-example-2" class="carousel slide carousel-fade" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<?php
$count = count($the_query->posts);
for ($i=0; $i<$count; $i++) {
?>
<li id="#carousel-example-2" data-slide-to="<?php echo $i; ?>"<?php
if ($i == 0) {
?> class="active"<?php
}
?>></li>
<?php
}
?>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="lisbox">
<?php
$count = 0;
while ($the_query->have_posts()) {
$the_query->the_post();
?>
<div class="carousel-item<?php
if ($count == 0) {
echo ' active';
}
?>">
<?php
// code for displaying the content of the slide here
?>
</div>
<?php
$count++;
}
?>
</div><!-- end .carousel-inner -->
</div><!-- end #carousel-example-2 -->
<?php
wp_reset_postdata();
} // end if have posts
?>
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.