PHP Dynamic Image Gallery using Bootstrap 4 flexbox - php

How can I dynamic below html in foreach loop? I've an images array I can echo one image per loop but I'm not sure how can I loop two images per loop?
<div class="d-flex flex-row">
<div class="d-flex flex-column">
<img src="1" class="img-fluid">
<img src="2" class="img-fluid">
</div>
<div class="d-flex flex-column">
<img src="3" class="img-fluid">
<img src="4" class="img-fluid">
</div>
<div class="d-flex flex-column">
<img src="5" class="img-fluid">
<img src="6" class="img-fluid">
</div>
<div class="d-flex flex-column">
<img src="7" class="img-fluid">
<img src="8" class="img-fluid">
</div>
</div>
This what I tried.
<?php $images = get_field('image_gallery'); ?>
<?php if($images): ?>
<div class="d-flex flex-row">
<?php foreach( $images as $image ): ?>
<div class="d-flex flex-column">
<a data-fancybox="gallery" href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['url']; ?> " class='img-fluid' alt="">
</a>
<!-- This duplicates the image and need some break or continue statment -->
<a data-fancybox="gallery" href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['url']; ?>" class='img-fluid' alt="">
</a>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>

Use the following code. May be it help you
<?php $images = get_field('image_gallery'); ?>
<?php if($images): ?>
<div class="d-flex flex-row">
<?php for ($i=0; $i < count($images); $i++) { ?>
<div class="d-flex flex-column">
<a data-fancybox="gallery" href="<?php echo $image[$i]['url']; ?>">
<img src="<?php echo $image[$i]['url']; ?> " class='img-fluid' alt="">
</a>
<!-- This duplicates the image and need some break or continue statment -->
<a data-fancybox="gallery" href="<?php echo $image[$i+1]['url']; ?>">
<img src="<?php echo $image[$i+1]['url']; ?>" class='img-fluid' alt="">
</a>
</div>
<?php
$i++;
}
?>
</div>
<?php endif; ?>

You could use a for loop instead and iterate the loop every 2 images...
<?php $images = get_field('image_gallery'); ?>
<?php if($images): ?>
<div class="d-flex flex-row">
<?php for($i = 0; $i < count($images); $i+=2){ ?>
<?php $image = $images[i];?>
<div class="d-flex flex-column">
<a data-fancybox="gallery" href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['url']; ?> " class='img-fluid' alt="">
</a>
<?php if (!empty($images[i+1])) { ?>
<?php $nextImage = $images[i+1];?>
<a data-fancybox="gallery" href="<?php echo $nextImage['url']; ?>">
<img src="<?php echo $nextImage['url']; ?>" class='img-fluid' alt="">
</a>
<?php } ?>
</div>
<?php } ?>
</div>
<?php endif; ?>

Related

How to count foreach loop by range

I'm trying to make the carousel slide image slide by 4 images per slide. I want each slide to display four images, and then the next slide will display another four images. I hope I have explained well.
<?php
$i = 0;
foreach ($photos as $photo) {
if ($i == 0) {
?>
<div class="carousel-item active">
<ul class="thumbnails">
<li class="span3">
<div class="thumbnail">
<i class="tag"></i>
<a href="#">
<img src="<?php echo $photo->image_path(); ?>" width="125" height="125" class="img-thumbnail" alt="buy more"/>
</a>
</div>
</li>
</ul>
</div>
<?php
$i++;
} else {
if ($i != 0) {
?>
<div class="carousel-item">
<ul class="thumbnails">
<li class="span3">
<div class="thumbnail">
<i class="tag"></i>
<a href="#">
<img src="<?php echo $photo->image_path(); ?>" width="125" height="125" class="img-thumbnail" alt="buy more"/>
</a>
</div>
</li>
</ul>
</div>
<?php
}
$i++;
}
}
?>
I believe that will do the trick:
<?php
$numImagesPerSlide = 4;
foreach($photos as $k => $photo){
?>
<div class="carousel-item <?php echo ($k % $numImagesPerSlide == 0) ? 'active': '';?>">
<ul class="thumbnails">
<li class="span3">
<div class="thumbnail">
<i class="tag"></i>
<img src="<?php echo $photo->image_path(); ?>" width="125" height="125" class="img-thumbnail" alt="buy more"/>
</div>
</li>
</ul>
</div>
<?php
}
?>
The point is to use $k, whenever it is divisible by 4 (4, 8, 12, 16, ...), with the function $k % $numImagesPerSlide == 0 then you print the code you need.
I guess you will need an extra code, like
if ($k % $numImagesPerSlide == 0) {
//print the header of the carrousel
}

Undefined offset: 2 while using explode

I have $product_image='img1.png,img2.png,img3.png' as a string where I explode $product_image and show image as $pro_img[0], $pro_img[1] and $pro_img[2].
I get error
Message: Undefined offset: 2
if $pro_img[2] doesn't find any image.
I want to solve it using foreach loop but I don't have any idea how can I use foreach to fix this problem?
<?php $pro_img = explode(",",$product_image); ?>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<div class="pro-large text-center">
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[0]; ?>" style="width: 50%;height: 35%;" alt="">
</div>
</div>
<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
<div class="pro-large text-center">
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[1]; ?>" style="width: 50%;height: 35%;" alt="">
</div>
</div>
<div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">
<div class="pro-large text-center">
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[2]; ?>" style="width: 50%;height: 35%;" alt="">
</div>
</div>
</div>
<ul class="nav product-img-tab" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[0]; ?>" style="width: 50%;height: 96px;" alt="">
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[1]; ?>" style="width: 50%;height: 96px;" alt="">
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false">
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[2]; ?>" style="width: 50%;height: 96px;" alt="">
</a>
</li>
</ul>
Since you have various sections (home/profile/contact), that I assume you always want to display either with or without an image, I suggest using isset instead.
Isset simply checks if a key exists in the $pro_img array, and you can use that to decide whether or not to print the image tag:
<?php
$pro_img = explode(",",$product_image);
?>
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<div class="pro-large text-center">
<?php if (isset($pro_img[0])) : ?>
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[0]; ?>" style="width: 50%;height: 35%;" alt="">
<?php endif; ?>
</div>
</div>
<div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
<div class="pro-large text-center">
<?php if (isset($pro_img[1])) : ?>
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[1]; ?>" style="width: 50%;height: 35%;" alt="">
<?php endif; ?>
</div>
</div>
<div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">
<div class="pro-large text-center">
<?php if (isset($pro_img[2])) : ?>
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[2]; ?>" style="width: 50%;height: 35%;" alt="">
<?php endif; ?>
</div>
</div>
</div>
<ul class="nav product-img-tab" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">
<?php if (isset($pro_img[0])) : ?>
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[0]; ?>" style="width: 50%;height: 96px;" alt="">
<?php endif; ?>
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">
<?php if (isset($pro_img[1])) : ?>
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[1]; ?>" style="width: 50%;height: 96px;" alt="">
<?php endif; ?>
</a>
</li>
<li class="nav-item">
<a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false">
<?php if (isset($pro_img[2])) : ?>
<img src="<?php echo base_url(); ?>resource/product/<?php echo $pro_img[2]; ?>" style="width: 50%;height: 96px;" alt="">
<?php endif; ?>
</a>
</li>
</ul>

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/

For each loop into bootstrap slider

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

Categories