I need to create a PHP loop to output the following HTML code. So basically, the first div has 'item active' followed by 4 divs, and each subsequent div only has a class of 'item' followed by 4 divs inside.
<div class="item active">
<div data-target="#carousel" data-slide-to="0" class="thumb"><img src="http://placehold.it/100/e8117f/fff&text=Product+Main"></div>
<div data-target="#carousel" data-slide-to="1" class="thumb"><img src="http://placehold.it/100/00ffff/000&text=Product+Image+2"></div>
<div data-target="#carousel" data-slide-to="2" class="thumb"><img src="http://placehold.it/100/ff00ff/fff&text=Product+Image+3"></div>
<div data-target="#carousel" data-slide-to="3" class="thumb"><img src="http://placehold.it/100/ffff00/000&text=Product+Image+4"></div>
</div><!-- /item -->
<div class="item">
<div data-target="#carousel" data-slide-to="4" class="thumb"><img src="http://placehold.it/100/612b65/fff&text=Product+Image+5"></div>
<div data-target="#carousel" data-slide-to="5" class="thumb"><img src="http://placehold.it/100/00ffcc/000&text=Product+Image+6"></div>
<div data-target="#carousel" data-slide-to="6" class="thumb"><img src="http://placehold.it/100/db371b/fff&text=Product+Image+7"></div>
<div data-target="#carousel" data-slide-to="7" class="thumb"><img src="http://placehold.it/100/feb8aa/000&text=Product+Image+8"></div>
</div><!-- /item -->
<div class="item">
<div data-target="#carousel" data-slide-to="8" class="thumb"><img src="http://placehold.it/100/612b65/fff&text=Product+Image+5"></div>
<div data-target="#carousel" data-slide-to="9" class="thumb"><img src="http://placehold.it/100/00ffcc/000&text=Product+Image+6"></div>
<div data-target="#carousel" data-slide-to="10" class="thumb"><img src="http://placehold.it/100/db371b/fff&text=Product+Image+7"></div>
<div data-target="#carousel" data-slide-to="11" class="thumb"><img src="http://placehold.it/100/feb8aa/000&text=Product+Image+8"></div>
</div><!-- /item -->
So far I have:-
<?php
if( have_rows('image_gallery', $street) ): $thumb = 0;
while ( have_rows('image_gallery', $street) ) : the_row(); $thumb++; ?>
<?php if ($thumb % 4 != 0) { ?>
<div class="item active">
<div data-target="#carousel" data-slide-to="<?php echo $thumb; ?>" class="thumb"><img src="<?php the_sub_field('image'); ?>"></div>
</div>
<?php } else { ?>
<div class="item">
<div data-target="#carousel" data-slide-to="<?php echo $thumb; ?>" class="thumb"><img src="<?php the_sub_field('image'); ?>"></div>
</div>
<?php } ?>
<?php endwhile;
else :
endif; ?>
Which doesn't do what I am wanting, so any help would be much appreciated.
EDIT,
Current the above code produces:-
<div class="carousel-inner">
<div class="item active">
<div data-target="#carousel" data-slide-to="1" class="thumb"><img src="http://radleigh.dev/wp-content/uploads/2015/11/berryheath-street.jpg"></div>
</div>
<div class="item active">
<div data-target="#carousel" data-slide-to="2" class="thumb"><img src="http://radleigh.dev/wp-content/uploads/2015/11/bradwell-street.jpg"></div>
</div>
<div class="item active">
<div data-target="#carousel" data-slide-to="3" class="thumb"><img src="http://radleigh.dev/wp-content/uploads/2015/11/castlegreen-street.jpg"></div>
</div>
<div class="item">
<div data-target="#carousel" data-slide-to="4" class="thumb"><img src="http://radleigh.dev/wp-content/uploads/2015/11/langley-steet-2.jpg"></div>
</div>
<div class="item active">
<div data-target="#carousel" data-slide-to="5" class="thumb"><img src="http://radleigh.dev/wp-content/uploads/2015/11/langley-steet.jpg"></div>
</div>
<div class="item active">
<div data-target="#carousel" data-slide-to="6" class="thumb"><img src="http://radleigh.dev/wp-content/uploads/2015/11/queensbury-street.jpg"></div>
</div>
<div class="item active">
<div data-target="#carousel" data-slide-to="7" class="thumb"><img src="http://radleigh.dev/wp-content/uploads/2015/11/radleigh-street.jpg"></div>
</div>
<div class="item">
<div data-target="#carousel" data-slide-to="8" class="thumb"><img src="http://radleigh.dev/wp-content/uploads/2015/11/saxonfields-street.jpg"></div>
</div>
<div class="item active">
<div data-target="#carousel" data-slide-to="9" class="thumb"><img src="http://radleigh.dev/wp-content/uploads/2015/11/weaverspoint-street.jpg"></div>
</div>
<div class="item active">
<div data-target="#carousel" data-slide-to="10" class="thumb"><img src="http://radleigh.dev/wp-content/uploads/2015/11/wessington-street.jpg"></div>
</div>
</div>
You can do something like this:
<?php
$counter = 0;
if(have_rows('image_gallery', $street)){
while(have_rows('image_gallery', $street)){
if($counter == 0){
?>
<div class="item active">
<?php
}elseif($counter % 4 == 0){
?>
</div>
<div class="item">
<?php
}
?>
<div data-target="#carousel" data-slide-to="<?php echo $counter; ?>" class="thumb"><img src="<?php the_sub_field('image'); ?>"></div>
<?php
++$counter;
}
?>
</div>
<?php
}
?>
First of all I'd recommend you to start using some sort of templating engine, for example Smarty. Mixing html with php as you do is very bad practice. Code is hardly readable, and very uncomfortable for supporting. If you will have to fix or change something in such code after some time passes, you will find yourself suffering much.
Simplest what you can do - put all data in array and implode() it with "\n" before output. It's also much easier to debug such constructions, and you will have clear php without html mixins.
This code
$products = array(
array(
0 => "http://placehold.it/100/e8117f/fff&text=Product+Main",
1 => "http://placehold.it/100/00ffff/000&text=Product+Image+2",
2 => "http://placehold.it/100/ff00ff/fff&text=Product+Image+3",
3 => "http://placehold.it/100/ffff00/000&text=Product+Image+4",
),
array(
4 => "http://placehold.it/100/612b65/fff&text=Product+Image+5",
5 => "http://placehold.it/100/00ffcc/000&text=Product+Image+6",
6 => "http://placehold.it/100/db371b/fff&text=Product+Image+7",
7 => "http://placehold.it/100/feb8aa/000&text=Product+Image+8",
),
array(
8 => "http://placehold.it/100/612b65/fff&text=Product+Image+5",
9 => "http://placehold.it/100/00ffcc/000&text=Product+Image+6",
10 => "http://placehold.it/100/db371b/fff&text=Product+Image+7",
11 => "http://placehold.it/100/feb8aa/000&text=Product+Image+8",
),
);
$tpl = ' <div data-target="#carousel" data-slide-to="__ID__" class="thumb"><img src="__SRC__"></div>';
$ret = array();
foreach($products as $pk=>$p) {
$ret[] = '<div class="item '.($pk==0?'active':'').'">';
foreach($p as $id=>$src) {
$ret[] = str_replace(array('__ID__','__SRC__'), array($id, $src), $tpl);
}
$ret[] = '</div><!-- /item -->';
}
$html = implode("\n",$ret);
print $html;
produces this output
<div class="item active">
<div data-target="#carousel" data-slide-to="0" class="thumb"><img src="http://placehold.it/100/e8117f/fff&text=Product+Main"></div>
<div data-target="#carousel" data-slide-to="1" class="thumb"><img src="http://placehold.it/100/00ffff/000&text=Product+Image+2"></div>
<div data-target="#carousel" data-slide-to="2" class="thumb"><img src="http://placehold.it/100/ff00ff/fff&text=Product+Image+3"></div>
<div data-target="#carousel" data-slide-to="3" class="thumb"><img src="http://placehold.it/100/ffff00/000&text=Product+Image+4"></div>
</div><!-- /item -->
<div class="item ">
<div data-target="#carousel" data-slide-to="4" class="thumb"><img src="http://placehold.it/100/612b65/fff&text=Product+Image+5"></div>
<div data-target="#carousel" data-slide-to="5" class="thumb"><img src="http://placehold.it/100/00ffcc/000&text=Product+Image+6"></div>
<div data-target="#carousel" data-slide-to="6" class="thumb"><img src="http://placehold.it/100/db371b/fff&text=Product+Image+7"></div>
<div data-target="#carousel" data-slide-to="7" class="thumb"><img src="http://placehold.it/100/feb8aa/000&text=Product+Image+8"></div>
</div><!-- /item -->
<div class="item ">
<div data-target="#carousel" data-slide-to="8" class="thumb"><img src="http://placehold.it/100/612b65/fff&text=Product+Image+5"></div>
<div data-target="#carousel" data-slide-to="9" class="thumb"><img src="http://placehold.it/100/00ffcc/000&text=Product+Image+6"></div>
<div data-target="#carousel" data-slide-to="10" class="thumb"><img src="http://placehold.it/100/db371b/fff&text=Product+Image+7"></div>
<div data-target="#carousel" data-slide-to="11" class="thumb"><img src="http://placehold.it/100/feb8aa/000&text=Product+Image+8"></div>
</div><!-- /item -->
See how much clearer code became? All you have to do - fill $products array properly.
Related
Given this HTML code, how can I create a dynamic Carosuel with thumbnails using Bootstrap 4 that can be updated in back-end using ACF Repeater?
My Repeater field is called carousel_repeater and inside I have an Image field called carosuel_image
HTML code
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="custCarousel" class="carousel slide" data-ride="carousel" align="center">
<!-- slides -->
<div class="carousel-inner">
<div class="carousel-item active"> <img src="https://i.imgur.com/weXVL8M.jpg" alt="Hills"> </div>
<div class="carousel-item"> <img src="https://i.imgur.com/Rpxx6wU.jpg" alt="Hills"> </div>
<div class="carousel-item"> <img src="https://i.imgur.com/83fandJ.jpg" alt="Hills"> </div>
<div class="carousel-item"> <img src="https://i.imgur.com/JiQ9Ppv.jpg" alt="Hills"> </div>
</div> <!-- Left right --> <a class="carousel-control-prev" href="#custCarousel" data-slide="prev"> <span class="carousel-control-prev-icon"></span> </a> <a class="carousel-control-next" href="#custCarousel" data-slide="next"> <span class="carousel-control-next-icon"></span> </a> <!-- Thumbnails -->
<ol class="carousel-indicators list-inline">
<li class="list-inline-item active"> <a id="carousel-selector-0" class="selected" data-slide-to="0" data-target="#custCarousel"> <img src="https://i.imgur.com/weXVL8M.jpg" class="img-fluid"> </a> </li>
<li class="list-inline-item"> <a id="carousel-selector-1" data-slide-to="1" data-target="#custCarousel"> <img src="https://i.imgur.com/Rpxx6wU.jpg" class="img-fluid"> </a> </li>
<li class="list-inline-item"> <a id="carousel-selector-2" data-slide-to="2" data-target="#custCarousel"> <img src="https://i.imgur.com/83fandJ.jpg" class="img-fluid"> </a> </li>
<li class="list-inline-item"> <a id="carousel-selector-2" data-slide-to="3" data-target="#custCarousel"> <img src="https://i.imgur.com/JiQ9Ppv.jpg" class="img-fluid"> </a> </li>
</ol>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="custCarousel" class="carousel slide" data-ride="carousel" align="center">
<div class="carousel-inner">
<?php
if( have_rows('carousel_repeater') ):
// Loop through rows.
while( have_rows('carousel_repeater') ) : the_row();
$sub_value = get_sub_field('sub_field');// whatever ur image field is called
?>
<div class="carousel-item active"> <img src="<?php echo $sub_value['url'] ?>g" alt="Hills"> </div>
<?php
// End loop.
endwhile;
?>
<ol class="carousel-indicators list-inline">
<li class="list-inline-item active"> <a id="carousel-selector-0" class="selected" data-slide-to="0" data-target="#custCarousel"> <img src="https://i.imgur.com/weXVL8M.jpg" class="img-fluid"> </a> </li>
<li class="list-inline-item"> <a id="carousel-selector-1" data-slide-to="1" data-target="#custCarousel"> <img src="https://i.imgur.com/Rpxx6wU.jpg" class="img-fluid"> </a> </li>
<li class="list-inline-item"> <a id="carousel-selector-2" data-slide-to="2" data-target="#custCarousel"> <img src="https://i.imgur.com/83fandJ.jpg" class="img-fluid"> </a> </li>
<li class="list-inline-item"> <a id="carousel-selector-2" data-slide-to="3" data-target="#custCarousel"> <img src="https://i.imgur.com/JiQ9Ppv.jpg" class="img-fluid"> </a> </li>
</ol>
</div>
</div>
</div>
</div>
<?php
endif;
?>
Probs something like this, your gna need to play with it as i havent tested it but this is a start, would recommend you look into the documentation for ACF repeaters
https://www.advancedcustomfields.com/resources/repeater/
Here is my Bootstrap ui that the data is fetched from the database. As you can see, the carousel is going down and cannot click next it just goes down. from the page.
1
please see the image above
data that I'm fetching are texts and not image
and here is my code
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Carousel indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<?php
$dataid;
$data = mysqli_query($con,"SELECT * FROM feedback");
$count = mysqli_num_rows($data);
if ($count != 0) {
echo '
<div class="row ">';
while($row = mysqli_fetch_array($data)) {
echo '
<!-- Wrapper for carousel items -->
<div class="carousel-inner">
<div class="item carousel-item active">
<p class="testimonial">'. $row['comment'] .'</p>
<p class="overview"><b>Anonymous</b></p>
<div class="star-rating">
<ul class="list-inline">
<li class="list-inline-item"><i class="fa fa-heart" ></i></li>
<li class="list-inline-item"><i class="fas fa-thumbtack" ></i></li>
<li class="list-inline-item"><i class="fas fa-archive"></i></li>
<li class="list-inline-item"><i class="fa fa-trash" ></i></li>
<li class="list-inline-item"><i class="fa fa-star-o"></i></li>
</ul>
</div>
</div>
</div>
';
}
}
?>
<!-- Carousel controls -->
<a class="carousel-control left carousel-control-prev" href="#myCarousel" data-slide="prev">
<i class="fa fa-angle-left"></i>
</a>
<a class="carousel-control right carousel-control-next" href="#myCarousel" data-slide="next">
<i class="fa fa-angle-right"></i>
</a>
</div>
</div>
The carousal structure is like
<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="..." alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="Third slide">
</div>
</div>
</div>
so you need to repeat the carousel-item
<div id="carouselExampleSlidesOnly" class="carousel slide" data-ride="carousel">
<div class="carousel-inner">
<?php while($row = mysqli_fetch_array($data)) { ?>
<div class="carousel-item">
<img class="d-block w-100" src="..." alt="First slide">
</div>
<?php } ?>
</div>
</div>
If you need to display the text in the Carousel that comes from database then just give some padding and background-color to the Image
<div class="carousel-item active">
<img src="<?php echo imageSrcFromDb; ?>" width="1100" height="500">
<div class="carousel-caption">
<h3><?php echo ; //Your Heading text ?></h3>
<p><?php echo ; //Some description ?></p>
</div>
</div>
<!-- Wrapper for carousel items -->
<div class="carousel-inner">
<?php
while($row = mysqli_fetch_array($data)) {
echo '
<div class="item carousel-item">
<p class="testimonial">'. $row['comment'] .'</p>
<p class="overview"><b>Anonymous</b></p>
<div class="star-rating">
<ul class="list-inline">
<li class="list-inline-item"><i class="fa fa-heart" ></i></li>
<li class="list-inline-item"><i class="fas fa-thumbtack" ></i></li>
<li class="list-inline-item"><i class="fas fa-archive"></i></li>
<li class="list-inline-item"><i class="fa fa-trash" ></i></li>
<li class="list-inline-item"><i class="fa fa-star-o"></i></li>
</ul>
</div>
</div>
';
}
?>
</div>
the while loop should be inside the class carousel-inner .
I have a carousel with four items for which each should display an image covering the background in full width. How can I set the post-thumbnail image to a background image as it does apparently not recall the css?
CSS
#services .item:nth-child(1) {
background-position: center;
background-repeat: no-repeat;
background-attachment: scroll !important;
background-size: cover !important;
width: 100%; }
HTML/PHP
<section id="services" class="services"><h1>What We Offer</h1>
<div class="products" >
<div class="carousel slide carousel-fade" id="carousel2" data-ride="carousel">
<div class="carousel-inner">
<?php
$i=1;
$the_query = new WP_Query(array(
‘category_name’ => ‘Cars’,
‘post_per_page’ => 4,
'tag' => 'services'
));
while ( $the_query->have_posts() ) :$the_query->the_post();if($i % 2 != 0){ ?>
<div class="item services active ">
<?php if (the_post_thumbnail()) : ?>
<div class="item services active">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<div class="content-left">
<div class="text-center">
<h3><?php the_title();?></h3>
<h2 class="text-center"><?php the_content();?></h2>
</div>
</div>
</div>
<?php }
else { ?>
<div class="item services">
<?php if (the_post_thumbnail()) : ?>
<div class="item services">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<div class="content-right">
<div class="text-center">
<h3><?php the_title();?></h3>
<h2 class="text-center"><?php the_content();?></h2>
</div>
</div>
</div>
<?php } $i++; endwhile; wp_reset_postdata();?>
</div>
</div>
</div>
<a class="left carousel-control" data-slide="prev" href="services #carousel2" role="button" >
<span class="glyphicon glyphicon-menu-left"></span>
</a>
<a class="right carousel-control" data-slide="next" href="services #carousel2" role="button" >
<span class="glyphicon glyphicon-menu-right"></span>
</a>
<ol class="carousel-indicators">
<li data-target="services #carousel2" data-slide-to="0" class="active"></li>
<li data-target="services #carousel2" data-slide-to="1"></li>
<li data-target="services #carousel2" data-slide-to="2"></li>
</ol>
</section><!--#services -->
I come from a Java background and never really have the need for foreach loops, but due to career and personal study I am getting heavily into php. I am still trying to get my head around them and am struggling to incorporate a conditional within it. I believe I may use to use a key array but I am completely lost. If you could explain how to achieve this, plus best practice.
Here you can the foreach loop I am trying to incorporate:
<?php foreach ($products as $product) { ?>
<?php if ($product['thumb']) { ?>
<div class="image"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" /></div>
<?php } ?>
<?php } ?>
Into this:
<div class="container">
<div class="col-md-12">
<h1>In the Spotlight</h1>
<div class="well">
<div id="myCarousel" class="carousel slide">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Carousel items -->
<div class="carousel-inner">
<div class="item active">
<div class="row">
<div class="col-md-3"><img class="img-responsive" src="http://placehold.it/250x250" alt="Image" style="max-width:100%;">
</div>
<div class="col-md-3"><img class="img-responsive" src="http://placehold.it/250x250" alt="Image" style="max-width:100%;">
</div>
<div class="col-md-3"><img class="img-responsive" src="http://placehold.it/250x250" alt="Image" style="max-width:100%;">
</div>
<div class="col-md-3"><img class="img-responsive" src="http://placehold.it/250x250" alt="Image" style="max-width:100%;">
</div>
</div>
<!--/row-->
</div>
<!--/item-->
<div class="item">
<div class="row">
<div class="col-md-3"><img class="img-responsive" src="http://placehold.it/250x250" alt="Image" style="max-width:100%;">
</div>
<div class="col-md-3"><img class="img-responsive" src="http://placehold.it/250x250" alt="Image" style="max-width:100%;">
</div>
<div class="col-md-3"><img class="img-responsive" src="http://placehold.it/250x250" alt="Image" style="max-width:100%;">
</div>
<div class="col-md-3"><img class="img-responsive" src="http://placehold.it/250x250" alt="Image" style="max-width:100%;">
</div>
</div>
<!--/row-->
</div>
<!--/item-->
<div class="item">
<div class="row">
<div class="col-md-3"><img class="img-responsive" src="http://placehold.it/250x250" alt="Image" style="max-width:100%;">
</div>
<div class="col-md-3"><img class="img-responsive" src="http://placehold.it/250x250" alt="Image" style="max-width:100%;">
</div>
<div class="col-md-3"><img class="img-responsive" src="http://placehold.it/250x250" alt="Image" style="max-width:100%;">
</div>
<div class="col-md-3"><img class="img-responsive" src="http://placehold.it/250x250" alt="Image" style="max-width:100%;">
</div>
</div>
<!--/row-->
</div>
<!--/item-->
</div>
<!--/carousel-inner--> <a class="left carousel-control" href="#myCarousel" data-slide="prev">‹</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">›</a>
</div>
<!--/myCarousel-->
</div>
<!--/well-->
</div>
</div>
Thanks in advance to those who help
This is a rough example of how you can do it. Its pretty silly but it does the job and you should be able to understand it.
<?php
// echo code from the start .. until <div class="carousel-inner"> <div class="item active">
$inserted = 0;
foreach ($products as $product) {
if (array_key_exists('thumb', $product) && !empty($product['thumb'])) {
++$inserted;
// echo '... code from each row, e.g. <div class="col-md-3" ....',
if ($insert % 4 == 0) { // call this for every 4 images
// echo the end of the row
}
if ($insert == 12) {
// echo the final part because we displayed 12 items now
break; // exit the loop after the 12 items
} else {
// echo the start of a row again because we haven't got 12 yet
}
}
}
?>
I've created Carousel which displays 4 thumbnails per slide and I have two slides.
<div class="container">
<div class="row">
<div class="carousel slide span8" id="myCarousel">
<div class="carousel-inner">
<div class="item active">
<ul class="thumbnails">
<li class="span2">
<div class="thumbnail">
<img src="http://placehold.it/260x180" alt="">
</div>
</li>
<li class="span2">
<div class="thumbnail">
<img src="http://placehold.it/260x180" alt="">
</div>
</li>
<li class="span2">
<div class="thumbnail">
<img src="http://placehold.it/260x180" alt="">
</div>
</li>
<li class="span2">
<div class="thumbnail">
<img src="http://placehold.it/260x180" alt="">
</div>
</li>
</ul>
</div>
<div class="item">
<ul class="thumbnails">
<li class="span2">
<div class="thumbnail">
<img src="http://placehold.it/260x180" alt="">
</div>
</li>
<li class="span2">
<div class="thumbnail">
<img src="http://placehold.it/260x180" alt="">
</div>
</li>
<li class="span2">
<div class="thumbnail">
<img src="http://placehold.it/260x180" alt="">
</div>
</li>
<li class="span2">
<div class="thumbnail">
<img src="http://placehold.it/260x180" alt="">
</div>
</li>
</ul>
</div>
</div>
<a data-slide="prev" href="#myCarousel" class="left carousel-control">‹</a>
<a data-slide="next" href="#myCarousel" class="right carousel-control">›</a>
</div>
</div>
These slides are populated with images from database using codeigniter. Now question is, if I want to create 6-7 slides and I don't want to create them all manually how should I go about it in code. So when I click left arrow new set of images is loaded.
Find the common denominator between all the images. In other words, this code snippet:
<li class="span2">
<div class="thumbnail">
<img src="IMAGE_URL" alt="">
</div>
</li>
Since that is standard and not changing for each image, you can print it out in a foreach loop. Query the image urls from the database into an array, then run your foreach loop inside of the html:
<div class="carousel-inner">
<div class="item active">
<ul class="thumbnails">
<?php foreach($image_url as $image) { ?>
<li class="span2">
<div class="thumbnail">
<img src="<?php echo $image; ?>" alt="">
</div>
</li>
<?php } ?>
</ul>
</div>
Twitter Bootstrap Carousel - displaying multiple thumbnails in Wordpress
<div class="container">
<!-- Carousel -->
<div id="promo-carousel" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php
// Item size (set here the number of posts for each group)
$i = 4;
// Set the arguments for the query
global $post;
$args = array(
'numberposts' => -1, // -1 is for all
'post_type' => 'post', // or 'post', 'page'
'orderby' => 'title', // or 'date', 'rand'
'order' => 'ASC', // or 'DESC'
);
// Get the posts
$myposts = get_posts($args);
// If there are posts
if($myposts):
// Groups the posts in groups of $i
$chunks = array_chunk($myposts, $i);
/*
* Item
* For each group (chunk) it generates an item
*/
foreach($chunks as $chunk):
// Sets as 'active' the first item
($chunk === reset($chunks)) ? $active = "active" : $active = "";
echo '<div class="item '.$active.'"><div class="container"><div class="row">';
/*
* Posts inside the current Item
* For each item it generates the posts HTML
*/
foreach($chunk as $post):
echo '<div class="col-xs-12 col-sm-6 col-md-3 col-lg-3">';
the_post_thumbnail();
echo '</div>';
endforeach;
echo'</div></div></div>';
endforeach;
// Prints the HTML
endif;
?>
</div> <!-- carousel inner -->
<!-- Controls -->
<a class="left carousel-control" href="#promo-carousel" role="button" data-slide="prev">
<span class="fa fa-arror-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#promo-carousel" role="button" data-slide="next">
<span class="fa fa-arror-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div> <!-- /carousel -->
</div>