doutan1637 2019-04-26 13:23
浏览 72

将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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料