doujiao9866 2015-12-15 08:52
浏览 100
已采纳

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

I have a repeating field in a WordPress site via ACF repeating fields:

      <?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. 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. Here is what I have for the API code:

          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.

Thanks!

展开全部

  • 写回答

1条回答 默认 最新

  • dousi1994 2015-12-15 12:22
    关注

    The scope of videoID in your function is global. 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.

    Since the JSON returned from the API includes the id, it would be simplest to just use that:

    function response(jsonData) {
      jQuery('li.recent-episode-' + jsonData.id ).find('h5').html(jsonData.name);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部