将jQuery脚本移动到WordPress中页面模板中的页脚

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?

duanpin2034
duanpin2034 请查看developer.wordpress.org/reference/functions/...它可能有所帮助。
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问