0
I am trying to display the images in the Bootstrap Carrousel with while.
I can access the array with all urls, but I’m not able to insert the img tag in while, I tried while with foreach to display the images but it’s not working.
Already the indicators are displaying correctly but with an extra indicator, as I do not count the 0
?
<?php
$this_post_id = get_the_ID();
$urls =get_post_meta($this_post_id,'my-images', true);
$totalUrls = count($urls);//conta urls no array
$number = 0;
?>
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<?php while($number <= $totalUrls){ ?>
<li data-target="#myCarousel" data-slide-to="<?php echo $number++; ?>"></li>
<?php } ?>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php
while($number <= $totalUrls){
foreach($urls as $url){
?>
<div class="item active"> // como deixar classe active apenas na imagem ativa
<img src="<?php echo $url;?>" class="img-responsive" />
<div class="carousel-caption">
...
</div>
</div>
<?php $number++
} //end foreach
} //end while
?>
</div>
Updated
<?php
$this_post_id = get_the_ID();
$urls =get_post_meta($this_post_id,'my-image-for-post', true);
$totalUrls = count($urls);//count urls in array
$number = 0;
?>
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<?php while($number < $totalUrls){ ?>
<li data-target="#myCarousel" data-slide-to="<?php echo $number++; ?>"></li>
<?php } ?>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php foreach($urls as $url){ ?>
<div class="item active">
<img src="<?php echo $url;?>" class="img-responsive" />
<div class="carousel-caption">
...
</div>
</div>
<?php } ?>
With the help of user Anderson Carlos Woss,
Fixed the conditional for: while $number be <
that $totalUrls, that fixed the problem in the indicators.
I left the while only in the indicators, the foreach to display the images and I can see all pictures there if they have all with the class active
in <div class="item active">
How do I leave only 1 image with the active class inside the foreach?
I appreciate help
With the
while
andforeach
, you would be going through the list twice, if the value of$number
was correct, which I don’t think. In the firstwhile
, you increase the value of$number
, then in the secondwhile
,$number
is already more than$totalUrls
, not getting into the loop. Start by trying to take out the secondwhile
.– Woss
Thank you @Andersoncarloswoss managed to solve the repetitions, updated my question, just leaves a single image with the class "active"
– Gislef
Thank you @Andersoncarloswoss solved, I put an answer
– Gislef