ACF value into js/jquery - YouTube fullscreen video

问题: I'm not sure if i can ask this way, but i will try. I use this YouTube fullscreen jquery plugin - Link The problem is that i have to enter the YouTube video ID manually in...

问题:

I'm not sure if i can ask this way, but i will try.
I use this YouTube fullscreen jquery plugin - Link
The problem is that i have to enter the YouTube video ID manually in the script. How do i get my ACF value to replace the videoId?

I have tried like this

<script>

  var jsVideo = <?php echo get_field('hero_baggrundsvideo'); ?>;

(function($) {
  $('#heroVideo').YTPlayer({
    fitToBackground: true,
    videoId: ''.$jsVideo.''
  });
})(jQuery);
</script>

But it does not work. I have placed this script inside my php file.
I hope you get what i am trying to say.

Please correct me, if my question is wrong.


回答1:

Above everything else, your JS code has syntax problems.

Dot . concatenates strings in PHP, Js uses +. You are not adding quotes around the video id. Try this and check the developer console for other errors.

<script>

  var jsVideo = "<?php echo get_field('hero_baggrundsvideo'); ?>";
  console.log("I am playing: "+jsVideo);

(function($) {
    $('#heroVideo').YTPlayer({
    fitToBackground: true,
    videoId: jsVideo;
 });
})(jQuery);
</script>
  • 发表于 2019-02-14 17:10
  • 阅读 ( 200 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除