pierrebalian of Rocket55 Web Team
7/12/2017 - 6:30 PM

pull youtube thumbnail and url out of ACF oEmbed field for use

pull youtube thumbnail and url out of ACF oEmbed field for use

<?php

            // check if the repeater field has rows of data
        if( have_rows('videos') ):

             // loop through the rows of data
          while ( have_rows('videos') ) : the_row(); 

        $video_url = get_sub_field('video_item', false, false);

        $video = get_sub_field( 'video_item' );

        preg_match('/src="(.+?)"/', $video, $matches_url );
        $src = $matches_url[1]; 

        preg_match('/embed(.*?)?feature/', $src, $matches_id );
        $id = $matches_id[1];
        $id = str_replace( str_split( '?/' ), '', $id );

        ?>
        
         <!--video item-->

        <div class="grid-item<?php if (get_sub_field('video_size') == 'large'): ?> lg<?php else: ?><?php endif; ?>">
        <a data-fancybox href="<?php echo $video_url; ?>">
            <img src="http://img.youtube.com/vi/<?php echo $id; ?>/maxresdefault.jpg">
          </a>
        </div>

      <?php endwhile;

      else :

           // no rows found

        endif;

      ?>