I am using ACF Pro to make a slider carousel with Slick Slider. I can get it to work with a basic gallery field but I am not even getting it to show when I try to display a repeater field with several sub fields. I am not sure exactly what I am doing wrong but I have been playing with this for a while, and I feel like I am kinda' half there.
Working PHP
<!-- Slider 1 -->
<?php
//Fields
//slider_portfolio = Gallery Field
$images = get_field('slider_portfolio');
if( $images ): ?>
<div class="slider-for">
<?php foreach( $images as $image ): ?>
<div class="slick-container">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
Non-Working PHP
This is the version I am trying to get to work. It is currently one image sub field inside a repeater field.
Fields:
slider_image = field (repeater field)
portfolio_slider = subfield (image field)
<!-- Slider 2 -->
<?php
//Image Slider
//slider_image = field (repeater field)
//portfolio_slider = subfield (image field)
function agero_slider() {
if( have_rows('slider_image') ):
echo '<div class="slider-for">';
// loop through the rows of data
while ( have_rows('slider_image') ) : the_row();
// display a sub field value
//vars
$image = get_sub_field('portfolio_slider');
?>
<div><img src="<?php echo $image['url']; ?>"/></div>
<?php
endwhile;
echo '</div>
<div class="slider-nav">';
// loop through the rows of data
while ( have_rows('slider_image') ) : the_row();
// display a sub field value
//vars
$image = get_sub_field('portfolio_slider');
?>
<div><img src="<?php echo $image['url']; ?>"/></div>
<?php
endwhile;
echo '</div>';
else :
// no rows found
endif;
}
?>
JS Code This works, even though I need to build on it.
jQuery(document).ready(function($){
$('.slider-for').slick({
slidesToShow: 1,
slidesToScroll: 1,
dots: true,
arrows: false,
asNavFor: '.slider-for',
focusOnSelect: true,
fade: true,
autoplay: false
});
});
I'm not sure why the first code is working but the second is not.
Relevant links:
ACF - https://www.advancedcustomfields.com/resources/code-examples/
Slick - http://kenwheeler.github.io/slick/
The Tutorial that I am using as a rough guide to get started -
http://wpbeaches.com/coding-a-slider-with-slick-and-acf-pro-in-wordpress/
If anyone can suggest what I am doing wrong or maybe suggest a concrete way to display multiple custom fields within a multi slide carousel where the text fields change with each slide image. Once I get this working I can expand it by making each repeater field a slide that can be updated through the UI.
I think maybe I am going about this wrong. Maybe I need to add everything inside a flexible content field or something? Not sure right now, but the second example seems like it should work.
get_field()
to get the slider field, it will return a data structure that contains all child-fields, no matter the level of depth. Run$data = get_field('slider_field')
, whereslider_field
is the name of the top-level slider field, and then callvar_dump($data)
, and then view the page source, and you'll be able to see a structured representation of the data contained in the structure, allowing you to get an idea of how to loop through it all. - thephpdev