I was wondering if and how it would be possible to take parameters from an Advanced Custom Field on a specific page template for some settings for a carousel (SlickSlider & jQuery).
Basically, what I don't want to do is just insert the script in the middle of the page.
For example:
PHP:
<?php
$class_name = get_field('class_name');
$number_slides = get_field('number_of_slides');
$slides_show = get_field('slides_to_show');
?>
<script>
$('.<?php echo $class_name;?>').slick({
infinite: true,
slidesToShow: <?php echo $number_slides; ?>,
slidesToScroll: <?php echo $slides_show; ?>
});
</script>
What I'm wonder is if it's possible to do an add_action
of sorts to push that script to the footer, but without it being in the functions.php
file.
For example:
PHP:
<?php
add_action('wp_footer', function () {
?>
<script>
$('.<?php echo $class_name;?>').slick({
infinite: true,
slidesToShow: <?php echo $number_slides; ?>,
slidesToScroll: <?php echo $slides_show; ?>
});
</script>
<?php
});
?>
^^^I know that's grossly terrible, but I'm just trying to illustrate my thought.
I guess the thing that I'm not sure about is how to pass something like that to the functions while grabbing the fields from ACF. Thing is that I want to reuse this on other page possibly, so trying to grab the page ID to pass those ACF items would be tricky unless there was a way to check what page it's on and then only render the script on that specific page.
The other thing I was looking into is maybe a do_action
of sorts and then snagging the variables from ACF and then passing them to the add_action
in the functions file. But, again, not sure if that's possible or not.
Anyways, I'm rambling and it's early, and I need more coffee. So, just trying to see if something like that is possible by chance?