简体   繁体   English

通过ACF从WordPress的重复字段中获取Brightcove视频

[英]Get Brightcove Videos from repeating field in WordPress via ACF

I have a repeating field in a WordPress site via ACF repeating fields: 我通过ACF重复字段在WordPress站点中有一个重复字段:

      <?php
      if( have_rows('recent_episodes') ):
        $i = 1;
          while ( have_rows('recent_episodes') ) : the_row();

        $image = get_sub_field('video_image');
      ?>

      <!--API CODE HERE-->

        <li class="recent-episode-<?php the_sub_field('brightcove_video_id'); ?> col-md-3 col-sm-4">
          <a href="#">
            <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
            <div class="flex-caption">
              <h6 class="date"><?php the_sub_field('air_date') ?></h6>
              <h5 class="title"></h5>
            </div>
          </a>
        </li>
      <?php
      $i++;
      endwhile; else :
      endif;
      ?>

The brightcove_video_id field is a video ID for a video in Brightcove. brightcove_video_id字段是Brightcove中视频的视频ID。 Ideally, I would like to pull in the corresponding title for each video in the repeating field. 理想情况下,我想在重复字段中为每个视频输入相应的标题。 When I put the API code in the repeating area though, it just outputs the last item each time. 但是,当我将API代码放在重复区域中时,它每次只会输出最后一个项目。 Here is what I have for the API code: 这是我对API代码的要求:

          var token = "HIDDEN";

          var req = "http://api.brightcove.com/services/library?"
          req += "command=find_video_by_id&token=" + encodeURIComponent(token);
          req += "&video_id=" + videoID;
          req += "&video_fields=id%2Cname%2CshortDescription%2CreferenceId&media_delivery=default//";
          req += "&callback=response";

          // Create a new request object
          bObj = new JSONscriptRequest(req);
          // Build the dynamic script tag
          bObj.buildScriptTag();
          // Add the script tag to the page
          bObj.addScriptTag();

        function response(jsonData) {
          jQuery('li.recent-episode-' + videoID ).find('h5').html(jsonData.name);
        }

I've tried using an each function as well without avail. 我已经尝试过使用每个功能以及无济于事。 I am sure I am just missing something in the Javascript, but you could use some help figuring it out. 我确定我只是在Javascript中缺少了一些东西,但是您可以使用一些帮助来解决它。

Thanks! 谢谢!

The scope of videoID in your function is global. 您的函数中videoID的范围是全局的。 You're updating its value each time you iterate through the script. 每次迭代脚本时,您都在更新其值。 By the time the callbacks are triggered (asynchronously) it's looped through and videoID has the last value you last set it to. 在回调被触发时(异步),它已经遍历了,并且videoID具有您上次将其设置为的最后一个值。

Since the JSON returned from the API includes the id, it would be simplest to just use that: 由于从API返回的JSON包含ID,因此仅使用ID将是最简单的:

function response(jsonData) {
  jQuery('li.recent-episode-' + jsonData.id ).find('h5').html(jsonData.name);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM