Advanced custom fields bootstrap slider active state - php

I have a slider built in bootstrap and using Advanced Custom Fields in WordPress, my data attribute when written in HTMl the data-slide-to="" would be 0, 1 ,2 ,3 and so on.
If i was to write this in HTML it works. now I have integrated it with WordPress Advanced Custom Fields and added php I can now inspect the code and see the data attribute having 0, 1, 2, 3 onward. however now i click on the div with this attribute and the slide wont change as it did written in HTML.
is there something in my code preventing this, why wont my slide change now if click on the div with a different data-slide-to=""?
For example in HTML if i click on the div with data-slide-to="1" then it shows the second slide.
however now written in PHP this in not working, can some one help me understand why?
my code
<?php
$sliderImages = get_field('slider');
$count = 0;
?>
<section class="pb-5">
<div class="container">
<div id="carousel_03" class="carousel slide" data-ride="carousel">
<div class="row">
<div class="col-lg-4">
<ol class="carousel-indicators tabs row">
<?php if (have_rows('slider')):
while (have_rows('slider')): the_row();
?>
<li class="col-lg-12 col-sm-6 mb-2 <?php echo ($count ++) >= 1 ? '' : 'active'; ?> ">
<div data-target="#carousel_3" data-slide-to="<?php echo ($count ++); ?>" role="button"
class="carousel-indicator p-3">
<h4 class="mb-1"><?php the_sub_field('title', $post->ID)?></h4>
<?php
endwhile;
endif;
?>
</div>
</li>
</ol>
</div>
<div class="col-lg-8 mb-3">
<div class="carousel-inner">
<?php foreach ($sliderImages as $imgNumber => $image) : ?>
<div class="carousel-item<?php if ($imgNumber === 0) : ?> active<?php endif ?>">
<img src="<?= $image['image']['url'] ?>" alt="<?= $image['image']['alt'] ?>">
</div>
<?php endforeach ?>
</div>
<a class="carousel-control-prev" href="#carousel_03" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only"></span>
</a>
<a class="carousel-control-next" href="#carousel_03" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only"></span>
</a>
</div>
</div>
</div>
</div>
</section>

Related

Advanced custom fields pro repeater, working with bootstrap slider

Hi guys im using WP advanced custom fields pro repeater function, trying get my bootstrap slider to work with advanced custom fields.
here is my ACF set up
ACF setup WP side
and here is my bootsrap code with ACF integrated.
my code
<section class="pb-5">
<div class="container">
<div id="carousel_03" class="carousel slide" data-ride="carousel">
<div class="row">
<div class="col-lg-4">
<ol class="carousel-indicators tabs row">
<li class="col-lg-12 col-sm-6 mb-2 active">
<div data-target="#carousel_03" data-slide-to="0" role="button" class="carousel-indicator p-3">
<h4 class="mb-1">one</h4>
</div>
</li>
<li class="col-lg-12 col-sm-6 mb-2">
<div data-target="#carousel_03" data-slide-to="1" role="button" class="carousel-indicator p-3">
<h4 class="mb-1">two</h4>
</div>
</li>
<li class="col-lg-12 col-sm-6 mb-2">
<div data-target="#carousel_03" data-slide-to="2" role="button" class="carousel-indicator p-3">
<h4 class="mb-1">three</h4>
</div>
</li>
<li class="col-lg-12 col-sm-6 mb-2">
<div data-target="#carousel_03" data-slide-to="3" role="button" class="carousel-indicator p-3">
<?php
if( have_rows('slider') ):
while ( have_rows('slider') ) : the_row();
the_sub_field( 'title');
?>
<h4 class="mb-1"><?php the_sub_field($section . 'title') ?></h4>
<?php
endwhile;
endif;
?>
</div>
</li>
</ol>
</div>
<div class="col-lg-8 mb-3">
<div class="carousel-inner">
<div class="carousel-item active">
<img src="some.svg" class="d-block w-100" alt="alt">
</div>
<div class="carousel-item">
<img src="some.svg" class="d-block w-100" alt="alt">
</div>
<div class="carousel-item">
<img src="some.svg" class="d-block w-100" alt="alt">
</div>
<div class="carousel-item">
<?php
if( have_rows('slider') ):
while ( have_rows('slider') ) : the_row();
the_sub_field( 'image');
?>
<img class="img-fluid"
src="<?php the_sub_field($section . 'image') ?>"
alt="<?php the_sub_field($section . 'alt') ?>">
<?php
endwhile;
endif;
?>
</div>
</div>
<a class="carousel-control-prev" href="#carousel_03" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only"></span>
</a>
<a class="carousel-control-next" href="#carousel_03" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only"></span>
</a>
</div>
</div>
</div>
</div>
</section>
My aim
for less code and to have ACF to pull all images for the slider in the correct order.
My problem
as I currently do not have my images in a array they all display where I cal my sub field, whats the best solution to get this working correctly ?
You are better off using get_field() (https://www.advancedcustomfields.com/resources/get_field/) to retrieve the repeater values in this instance, as this returns an associative array containing all sub-field values.
You can then iterate that array to output the HTML you need for the Bootstrap carousel.
Also worth noting that the image values returned will also contain a key sizes with URLs to the images of all of your themes thumbnail sizes in it, if you want to output a particular image size in the carousel.
Note: I haven't actually tested this with Bootstrap, but it should work.
<?php
// get the ACF values using get_field()
$sliderImages = get_field('slider');
?>
<section class="pb-5">
<div class="container">
<div id="carousel_03" class="carousel slide" data-ride="carousel">
<div class="row">
<div class="col-lg-4">
<ol class="carousel-indicators tabs row">
<!-- Iterate images to output indicators -->
<?php foreach ($sliderImages as $imgNumber => $image) : ?>
<li class="col-lg-12 col-sm-6 mb-2<?php if ($imgNumber === 0) : ?> active<?php endif ?>">
<div data-target="#carousel_<?= $imgNumber ?>" data-slide-to="<?= $imgNumber ?>" role="button" class="carousel-indicator p-3">
<h4 class="mb-1"><?= $imgNumber + 1 ?></h4>
</div>
</li>
<?php endforeach ?>
</ol>
</div>
<div class="col-lg-8 mb-3">
<div class="carousel-inner">
<!-- Iterate images again to output carousel items -->
<?php foreach ($sliderImages as $imgNumber => $image) : ?>
<div class="carousel-item<?php if ($imgNumber === 0) : ?> active<?php endif ?>">
<img src="<?= $image['image']['url'] ?>" alt="<?= $image['image']['alt'] ?>">
</div>
<?php endforeach ?>
</div>
<a class="carousel-control-prev" href="#carousel_03" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only"></span>
</a>
<a class="carousel-control-next" href="#carousel_03" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only"></span>
</a>
</div>
</div>
</div>
</div>
</section>

Issue while trying to display images from display on carousel slider

I currently trying to make it where My Database is storing image names like slider1.jpg and then I'm retrieving that from my database, but the issue is when I'm selecting * all from the table its only displaying the first link in the table. Any Ideas on how I can fix this in my code?
Heres my PHP:
<?php
//Gets Links
$stmt = $DB_con->prepare('SELECT * FROM slider');
$stmt->execute();
if($stmt->rowCount() > 0)
{
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
}
?>
And Heres my Carousel Slider.
<header>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">
<!-- Slide One - Set the background image for this slide in the line below -->
<div class="carousel-item active" style="background-image: url('images/slider/<?php echo $row['link'];?>')">
<div class="carousel-caption d-none d-md-block">
<h3>First Slide</h3>
<p>This is a description for the first slide.</p>
</div>
</div>
<!-- Slide Two - Set the background image for this slide in the line below -->
<div class="carousel-item" style="background-image: url('images/slider/<?php echo $row['link'];?>')">
<div class="carousel-caption d-none d-md-block">
<h3>Second Slide</h3>
<p>This is a description for the second slide.</p>
</div>
</div>
<!-- Slide Three - Set the background image for this slide in the line below -->
<div class="carousel-item" style="background-image: url('images/slider/<?php echo $row['link'];?>')">
<div class="carousel-caption d-none d-md-block">
<h3>Third Slide</h3>
<p>This is a description for the third slide.</p>
</div>
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</header>
You need to utilize a loop on the results so you can build each slide with a new $row from the database query:
<?php
// build a clean array of slides from the db grab
$stmt = $DB_con->prepare('SELECT * FROM slider');
$stmt->execute();
$slides = [];
if($stmt->rowCount() > 0) {
$slides = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
.... starter html ....
<ol class="carousel-indicators">
<?php foreach($slides as $i => $slide) { ?>
<li data-target="#carouselExampleIndicators"
data-slide-to="<?php echo $i;?>"
class="<?php echo (!$i?'active':'');?>"></li>
<?php }?>
</ol>
.... more html ....
<div class="carousel-inner" role="listbox">
<?php foreach($slides as $i => $slide) { ?>
<div class="carousel-item <?php echo (!$i?'active':'');?>"
style="background-image: url('images/slider/<?php echo $slide['link'];?>')">
<div class="carousel-caption d-none d-md-block">
<h3><?php echo $slide['slide_name'];?></h3>
<p><?php echo $slide['slide_desc'];?></p>
</div>
</div>
<?php } ?>
</div>
.... the rest of your html output ....

Want to make a dynamic grid in bootstrap slider in php

I have made a dynamic bootstrap slider according the data fetched from database in php. Here I want to display 3 grid per slider, it works completely. But I just want to create a 3 grid in a single row. Here i got the grid in new row. What I have to do to get the 3 grid in a single row?
<div class="container content">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
</ol>
<div class="col-lg-12">
<h1><center>Client Reviews</center></h1>
</div>
<div class="carousel-inner">
<?php
foreach($hello as $review){
?>
<?php if ($i % 3 == 0):?>
<div class="item<?php if ($is_active) echo ' active'?>">
<?php endif?>
<div class="row">
<div class="col-xs-12" style="margin:35px;">
<div class="col-md-4">
<div class="caption">
<p class="text-info lead adjust2"><?php echo $review\['testimonial'\];?></p>
<p><span class="glyphicon glyphicon-minus"></span> <?php echo $review\['name'\];?></p>
</div>
</div>
</div>
</div>
<?php if (($i+1) % 3 == 0 || $i == count($review)-1):?>
</div>
<?php endif?>
<?php
$i++;
if ($is_active) $is_active = false;
};
?>
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span> </a>
<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span> </a>
</div>
</div>
I think u open add before loop.
[![<div class="container content">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
</ol>
<div class="col-lg-12">
<h1><center>Client Reviews</center></h1>
</div>
<div class="carousel-inner">
<div class="row">
<div class="col-xs-12" style="margin:35px;">
<?php
foreach($hello as $review){
?>
<?php if ($i % 3 == 0):?>
<div class="item<?php if ($is_active) echo ' active'?>">
<?php endif?>
<div class="col-md-4">
<div class="caption">
<p class="text-info lead adjust2"><?php echo $review\['testimonial'\];?></p>
<p><span class="glyphicon glyphicon-minus"></span> <?php echo $review\['name'\];?></p>
</div>
</div>
<?php if (($i+1) % 3 == 0 || $i == count($review)-1):?>
</div>
<?php endif?>
<?php
$i++;
if ($is_active) $is_active = false;
};
?>
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span> </a>
<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span> </a>
</div>
</div>
</div>
</div>][1]][1]

how to make more than one bootstrap carousel slider dynamically on one page

I create more than one slider dynamically using php but prev or next button is working only in first slider and not in rest slider due to having same class in control, other ones are not working. Please help me to fix this.
here is my slider code with php
if($counter == 0)
{
//$image_path='<div class="panel-body" style="background-image: url('.$rowu['event_image'].');>';
$Indicators ='<li data-target="#carousel-example-generic" data-slide-to="'.$counter.'" class="active"></li>';
$slides = '<div class="item active" >
<div class="container-fluid" style="marign:0px;padding:0px; background-image: url('.$rowu['event_image'].');">
<div class="highlighted">'.$event_category.'</div>
<div class="highlighted">'.$event_city.'</div>
<div class="highlighted">'.date_format($startDate,'d F').'</div>
<div class="pull-right" style="margin-top:270px">
Book
</div>
</div>
</div>';
}
else{
//$image_path .='<div class="panel-body" style="background-image: url('.$rowu['event_image'].');>';
$Indicators .='<li data-target="#carousel-example-generic" data-slide-to="'.$counter.'"></li>';
$slides .= '<div class="item">
<div class="container-fluid" style="marign:0px;padding:0px; background-image: url('.$rowu['event_image'].');">
<div class="highlighted">'.$event_category.'</div>
<div class="highlighted">'.$event_city.'</div>
<div class="highlighted">'.date_format($startDate,'d F').'</div>
<div class="pull-right" style="margin-top:270px">
Book
</div>
</div>
</div>';
}
$counter++;
}
echo '
<div class="panel panel-default" style="font-family: Futura,Trebuchet MS,Arial,sans-serif;">
<!-- panel body where slider will work -->
<div class="panel-body">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
'.$Indicators.'
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
'.$slides.'
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
`
each slider should have a unique ID
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
as you see it controls #carousel-example-generic
Also it's same for bullets
<li data-target="#carousel-example-generic" data-slide-to="'.$counter.'"></li>
data-target define the slider.

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