For each loop into bootstrap slider - php

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
}
}
}
?>

Related

Bootstrap 4 Carosuel with ACF Repeater field

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/

Looping issue in fetching and display data in php

I have 24 records in database table. I want to display 6 records in each particular list. So, i need 4 list of records basically.
I have tried this way, refer below code snippet for more details.
<?php
$homeBrands1 =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.HOME_LOGO,b.STATUS,m.ID,m.TITLE,m.LOGO FROM cms as b
INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' AND b.POSITION=1");
$homeResult1 =mysql_fetch_array($homeBrands1);
$homeBrands3 =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.STATUS,b.HOME_LOGO,m.ID,m.TITLE,m.LOGO FROM cms as b
INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' AND b.POSITION=3");
$homeResult3 =mysql_fetch_array($homeBrands3);
$homeBrands2 =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.STATUS,b.HOME_LOGO,m.ID,m.TITLE,m.LOGO FROM cms as b
INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' AND b.POSITION=2");
$homeResult2 =mysql_fetch_array($homeBrands2);
$homeBrands4 =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.STATUS,b.HOME_LOGO,m.ID,m.TITLE,m.LOGO FROM cms as b
INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' AND b.POSITION=4");
$homeResult4 =mysql_fetch_array($homeBrands4);
$homeBrands5 =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.STATUS,b.HOME_LOGO,m.ID,m.TITLE,m.LOGO FROM cms as b
INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' AND b.POSITION=5");
$homeResult5 =mysql_fetch_array($homeBrands5);
$homeBrands6 =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.STATUS,b.HOME_LOGO,m.ID,m.TITLE,m.LOGO FROM cms as b
INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' AND b.POSITION=6");
$homeResult6 =mysql_fetch_array($homeBrands6);
?>
<div class="brands">
<div class="main-heading">
<h3>Our<br> Brands</h3>
</div>
<ul id="slider1" style="overflow:hidden;">
<li style="margin-left: -19px !important; margin-right:22px !important;">
<div class="row" style="padding-left:10px !important; margin-left:1px;">
<div class="col-sm-4">
<div class="brand-cat wow fadeInUp" data-wow-delay="0s">
<img src="images/brands/<?php echo $homeResult1['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult1['TITLE'];?>">
<div class="brand-name"><img src="images/home_logos/<?php echo $homeResult1['HOME_LOGO'];?>" class="img-responsive" alt=""></div>
<div class="overlay">
<div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult1['HOME_LOGO'];?>" class="img-responsive" alt=""><i class="fa fa-long-arrow-right" aria-hidden="true"></i></div>
View more
</div>
</div>
<div class="brand-cat wow fadeInUp" data-wow-delay="0.4s">
<img src="images/brands/<?php echo $homeResult3['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult3['TITLE'];?>">
<div class="brand-name"><img src="images/home_logos/<?php echo $homeResult3['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult3['TITLE'];?>"></div>
<div class="overlay">
<div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult3['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult3['TITLE'];?>"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></div>
View more
</div>
</div>
</div>
<div class="col-sm-8">
<div class="row">
<div class="col-sm-6">
<div class="brand-cat stradivarius wow fadeInUp" data-wow-delay="0.3s">
<img src="images/brands/<?php echo $homeResult2['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult2['TITLE'];?>">
<div class="brand-name"><img src="images/home_logos/<?php echo $homeResult2['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult2['TITLE'];?>"></div>
<div class="overlay">
<div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult2['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult2['TITLE'];?>"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></div>
View more
</div>
</div>
<div class="brand-cat wow fadeInUp" data-wow-delay="0.5s">
<img src="images/brands/<?php echo $homeResult4['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult4['TITLE'];?>">
<div class="brand-name"><img src="images/home_logos/<?php echo $homeResult4['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult4['TITLE'];?>"></div>
<div class="overlay">
<div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult4['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult4['TITLE'];?>"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></div>
View more
</div>
</div>
</div>
<div class="col-sm-6" style="padding-left:10px;">
<div class="brand-cat gerry wow fadeInUp" data-wow-delay="0.2s">
<img src="images/brands/<?php echo $homeResult5['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult5['TITLE'];?>">
<div class="brand-name"><img src="images/home_logos/<?php echo $homeResult5['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult5['TITLE'];?>"></div>
<div class="overlay">
<div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult5['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult5['TITLE'];?>"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></div>
View more
</div>
</div>
</div>
</div>
<div class="row" style="padding:0px !important;">
<div class="col-sm-12">
<div class="brand-cat wow fadeInUp" data-wow-delay="0.5s">
<img src="images/brands/<?php echo $homeResult6['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult6['TITLE'];?>">
<div class="brand-name"><img src="images/home_logos/<?php echo $homeResult6['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult6['TITLE'];?>"></div>
<div class="overlay">
<div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult6['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult6['TITLE'];?>"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></div>
View more
</div>
</div>
</div>
</div>
</div>
<div style="clear:both"></div>
</div>
<div style="clear:both"></div>
</li>
<li style="margin-left: -19px !important; margin-right:22px !important;">
<div class="row" style="padding-left:10px !important; margin-left:1px;">
<div class="col-sm-4">
<div class="brand-cat wow fadeInUp" data-wow-delay="0s">
<img src="images/brands/<?php echo $homeResult1['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult1['TITLE'];?>">
<div class="brand-name"><img src="images/home_logos/<?php echo $homeResult1['HOME_LOGO'];?>" class="img-responsive" alt=""></div>
<div class="overlay">
<div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult1['HOME_LOGO'];?>" class="img-responsive" alt=""><i class="fa fa-long-arrow-right" aria-hidden="true"></i></div>
View more
</div>
</div>
<div class="brand-cat wow fadeInUp" data-wow-delay="0.4s">
<img src="images/brands/<?php echo $homeResult3['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult3['TITLE'];?>">
<div class="brand-name"><img src="images/home_logos/<?php echo $homeResult3['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult3['TITLE'];?>"></div>
<div class="overlay">
<div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult3['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult3['TITLE'];?>"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></div>
View more
</div>
</div>
</div>
<div class="col-sm-8">
<div class="row">
<div class="col-sm-6">
<div class="brand-cat stradivarius wow fadeInUp" data-wow-delay="0.3s">
<img src="images/brands/<?php echo $homeResult2['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult2['TITLE'];?>">
<div class="brand-name"><img src="images/home_logos/<?php echo $homeResult2['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult2['TITLE'];?>"></div>
<div class="overlay">
<div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult2['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult2['TITLE'];?>"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></div>
View more
</div>
</div>
<div class="brand-cat wow fadeInUp" data-wow-delay="0.5s">
<img src="images/brands/<?php echo $homeResult4['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult4['TITLE'];?>">
<div class="brand-name"><img src="images/home_logos/<?php echo $homeResult4['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult4['TITLE'];?>"></div>
<div class="overlay">
<div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult4['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult4['TITLE'];?>"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></div>
View more
</div>
</div>
</div>
<div class="col-sm-6" style="padding-left:10px;">
<div class="brand-cat gerry wow fadeInUp" data-wow-delay="0.2s">
<img src="images/brands/<?php echo $homeResult5['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult5['TITLE'];?>">
<div class="brand-name"><img src="images/home_logos/<?php echo $homeResult5['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult5['TITLE'];?>"></div>
<div class="overlay">
<div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult5['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult5['TITLE'];?>"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></div>
View more
</div>
</div>
</div>
</div>
<div class="row" style="padding:0px !important;">
<div class="col-sm-12">
<div class="brand-cat wow fadeInUp" data-wow-delay="0.5s">
<img src="images/brands/<?php echo $homeResult6['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult6['TITLE'];?>">
<div class="brand-name"><img src="images/home_logos/<?php echo $homeResult6['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult6['TITLE'];?>"></div>
<div class="overlay">
<div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult6['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult6['TITLE'];?>"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></div>
View more
</div>
</div>
</div>
</div>
</div>
<div style="clear:both"></div>
</div>
<div style="clear:both"></div>
</li>
</ul>
</div>
Can anyone help me on this issue?
You only need to do one query, and save your results in an array:
$homeBrands =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.HOME_LOGO,b.STATUS,m.ID,m.TITLE,m.LOGO FROM cms as b
INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' ORDER BY b.POSITION LIMIT 6");
$homeResult =mysql_fetch_array($homeBrands);
Then iterate over the results using a loop (it is the loop that you are missing in your case):
<?php
foreach($homeResult as $hr)
{
?>
<div class="brand-cat wow fadeInUp" data-wow-delay="0s">
<img src="images/brands/<?php echo $hr['IMAGES']; ?>" class="img-responsive" alt="<?php echo $hr['TITLE'];?>">
<div class="brand-name"><img src="images/home_logos/<?php echo $hr['HOME_LOGO'];?>" class="img-responsive" alt=""></div>
<div class="overlay">
<div class="overly-brand-name"><img src="images/home_logos/<?php echo $hr['HOME_LOGO'];?>" class="img-responsive" alt=""><i class="fa fa-long-arrow-right" aria-hidden="true"></i></div>
View more
</div>
</div>
<?php
}
?>
And that's all the code you need, regardless of number of results you want to display. Hope you get the idea. The code is not tested

Carousel Slider with query loop inside for featured custom post type

So I'm trying to get a carousel slider to display 3 posts at a time through a loop for featured custom posts type.
This is the code that I have for the carousel:
<div class="container">
<div class="row">
<h2>Featured Posts</h2>
</div>
<div class='row'>
<div class='col-md-8'>
<div class="carousel slide media-carousel" id="media">
<div class="carousel-inner">
<div class="item active">
<div class="row">
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
</div>
</div>
<div class="item">
<div class="row">
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
</div>
</div>
<div class="item">
<div class="row">
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
</div>
</div>
</div>
<a data-slide="prev" href="#media" class="left carousel-control">‹</a>
<a data-slide="next" href="#media" class="right carousel-control">›</a>
</div>
</div>
</div>
</div>
I just want to display the thumbnails with the permalink for the following query:
<?php
$featured = new WP_Query( array(
'post_type' => 'franchisings',
'posts_per_page' => '3',
'meta_key' => 'meta-checkbox',
'meta_value' => 'yes'
) );
if ($featured->have_posts()): while($featured->have_posts()): $featured->
the_post(); ?>
<div class="row">
<div class="col-md-3">
<?php if (has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?></a>
</div>
</div>
<?php
endif;
endwhile; else:
endif;
?>
I don't want to use plugins, and I'm using the bootstrap framework. And this is what I'm going for, but with the featured posts' thumbnails instead:
I can get the featured post types to display but I can't get them properly inside the carousel.
Can you help out? Thanks
CODE UPDATED WITH #UCHENG ANSWER
<div class="container">
<div class="row">
<h3>
Franchisings em Destaque
</h3>
<div class='col-md-8'>
<div class="carousel slide media-carousel" id="media">
<div class="carousel-inner">
<div class="item active">
<?php
$featured = new WP_Query( array(
'post_type' => 'franchisings',
'posts_per_page' => '-1',
'meta_key' => 'meta-checkbox',
'meta_value' => 'yes'
) );
$counter = 0;
if ( $featured->have_posts()): while( $featured->have_posts() ): $featured->the_post(); ?>
<?php if ( $counter%3 == 0 ): ?>
<div class="item <?php if ( $counter == 0 ): ?>active<?php endif; ?>">
<div class="row">
<?php endif; ?>
<?php if ( has_post_thumbnail() ): ?>
<div class="col-md-4">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(array(200, 200)); ?>
<?php $counter++; ?>
</a>
</div>
<?php endif; ?>
<?php if ( $counter%3 == 0 ):?>
</div>
</div><!--end item-->
<?php endif; ?>
<?php endwhile;endif;
?>
You need to set posts_per_page as -1 to get all posts.
<div class="container">
<div class="row">
<h2>Featured Posts</h2>
</div>
<div class='row'>
<div class='col-md-8'>
<div class="carousel slide media-carousel" id="media">
<div class="carousel-inner">
<?php
$featured = new WP_Query( array(
'post_type' => 'franchisings',
'posts_per_page' => '-1',//-1 to get all posts
'meta_key' => 'meta-checkbox',
'meta_value' => 'yes'
) );
$counter = 0;
if ( $featured->have_posts()): while( $featured->have_posts() ): $featured->the_post(); ?>
<?php if ( $counter%3 == 0 ): ?>
<div class="item <?php if ( $counter == 0 ): ?>active<?php endif; ?>">
<div class="row">
<?php endif; ?>
<?php if ( has_post_thumbnail() ): ?>
<div class="col-md-4">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<?php $counter++; ?>
</a>
</div>
<?php endif; ?>
<?php if ( $counter%3 == 0 ):?>
</div>
</div><!--end item-->
<?php endif; ?>
<?php endwhile;endif;
</div><!--carousel-inner-->
</div>
</div>
</div><!--end row-->
</div>
There is no carousel component in Bootstrap 3, but you can try carousel in Bootstrap 4.
In most of my projects, I use Owl Carousel to make this effect. It' easy to use and has lots options to customize, of course, it's responsive.
Pre-Step
Please enqueue the owl-carousel js and css by using wp_enqueue_script hook.
The Html
<div class="container">
<div class="row">
<h2>Featured Posts</h2>
</div>
<div class='row'>
<div class='col-md-8'>
<div class="owl-carousel" id="media">
<div class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></div>
<div class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></div>
<div class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></div>
<div class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></div>
<div class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></div>
<div class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></div>
<div class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></div>
<div class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></div>
<div class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></div>
</div>
</div>
</div>
</div>
The CSS
#media {
width: 500px
}
The JS
$(document).ready(function() {
$("#media").owlCarousel({
items : 3,
});
});
The demo on CodePen: http://codepen.io/anon/pen/QGNqNa
For more customize options, please check owl-carousel website: http://owlgraphic.com/owlcarousel/

PHP Loop with multiples of

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.

Twitter Bootstrap Carousel - displaying multiple thumbnails

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>

Categories