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>
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/
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>
looking for a steer in the right direction getting this loop working. I'm nearly there but not 100% as I've got a mysterious box appearing at the top and the bottom of the search results. Would anyone be able to check over to see if the loop is right, please?
I've tried jigging the loop around but can't make any sense of it.
<?php get_header(); ?>
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
<p class="text-muted text-size-small">
<?php
global $wp_query;
echo 'You have '.$wp_query->found_posts.' results found.';?>
</p>
<hr>
<?
php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="media-body">
<h6 class="media-heading text-semibold">
<?php echo get_post_meta(get_the_id(), 'title', true ); ?>,
<?php echo get_post_meta(get_the_id(), 'firstname', true ); ?>,
<?php echo get_post_meta(get_the_id(), 'lastname', true ); ?>
</h6>
<ul class="list-inline list-inline-separate text-muted mb-10">
<li><a href="#" class="text-muted">
<?php echo get_post_meta( get_the_id(), 'type', true ); ?>
</a> </li>
<li><?php echo get_post_meta(get_the_id(), 'netbios_name', true ); ?> |
<?php echo get_post_meta(get_the_id(), 'device_class', true ); ?></li>
</ul>
<?php echo get_post_meta( get_the_id(), 'asset_id', true ); ?>
<?php echo get_post_meta(get_the_id(), 'asset_status', true ); ?>
</div>
<div class="media-right text-nowrap">
<a href="<?php the_permalink();?>"
class="btn btn-warning">View</a>
</div>
</li>
<ul class="media-list content-group">
<li class="media panel panel-body stack-media-on-mobile">
<div class="media-left">
<a href="#">
<img src="assets/images/demo/brands/dell.png" class="img-rounded img-lg" alt="">
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php get_template_part('footer', 'simple');?>
<?php get_footer(); ?>
Here's what the HTML view is spitting out
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-body">
<p class="text-muted text-size-small">
You have 3 results found. </p>
<hr>
<div class="media-body">
<h6 class="media-heading text-
semibold">
, , </h6>
<ul class="list-inline list-inline-
separate text-muted mb-10">
<li><a href="#" class="text-
muted"> </a></li>
<li> | </li>
</ul>
</div>
<div class="media-right text-nowrap">
View
</div>
</li>
<ul class="media-list content-group">
<li class="media panel panel-body stack-
media-on-mobile">
<div class="media-left">
<a href="#">
<img src=" class="img-rounded
img-lg" alt="">
</a>
</div>
<div class="media-body">
<h6 class="media-heading text-
semibold">
34
, , </h6>
<ul class="list-inline list-inline-
separate text-muted mb-10">
<li><a href="#" class="text-
muted"> </a></li>
<li> | </li>
</ul>
</div>
<div class="media-right text-nowrap">
<a href="" class="btn btn-
warning">View</a>
</div>
</li>
<ul class="media-list content-group">
<li class="media panel panel-body stack-
media-on-mobile">
<div class="media-left">
<a href="#">
<img src=" class="img-rounded
img-lg" alt="">
</a>
</div>
<div class="media-body">
<h6 class="media-heading text-
semibold">
34
Mr, User, User </h6>
<ul class="list-inline list-inline-separate text-muted mb-10">
<li>User </li>
<li> | </li>
</ul>
</div>
<div
class="media-right text-nowrap">
View
</div>
</li>
<ul class="media-list content-group">
<li class="media
panel panel-body stack-media-on-mobile">
<div
class="media-left">
<a
href="#">
<img src=" class="img-rounded img-lg" alt="">
</a>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- /main content -->
</div>
<!-- /page content -->
/div>
<!-- /page container -->
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]
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>