• Resolved knolan2

    (@knolan2)


    Hi I am trying to create a video gallery that opens in fancybox. I am almost there I inserted the youtube link in an link tag and it works. Now I just need to attach $video_thumbnail (the thumbnail) to the img tag src. I am somewhat new to php ..Here is my code:

    <ul class="filterable-grid clearfix">
         <?php /* Loop the stuff from the videos post type */
              $args = array( 'order'=> 'ASC', 'orderby' => 'title', 'post_type' => 'television', 'posts_per_page' => -1 );
              $loop = new WP_Query( $args );
              while ( $loop->have_posts() ) : $loop->the_post();?>
                   <li>
    
                   <?php
                   /* Set variables and create if stament */
    				$video_thumbnail = get_post_meta($post->ID, 'Video Thumbnail', single);
    				$videosite = get_post_meta($post->ID, 'Video Site', single);
    				$videoid = get_post_meta($post->ID, "Video ID", single);
    				if ($videosite == vimeo) {
      				echo '<iframe src="https://player.vimeo.com/video/'.$videoid.'" width="300" height="200" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
    				} else if ($videosite == youtube) {
      				echo '<a class="fancybox"  title="" href="https://www.youtube.com/watch?v='.$videoid.'?fs=1&autoplay=1"><img src="'<?php echo $video_thumbnail ?>'" /></a>';
    				} else {
      				echo 'Please Select Video Site Via the CMS';
    				}
    				?>
    
    			   </li>
    
         <?php endwhile;?>
    </ul>

    Right now I am just focusing on the youtube code. Every video post has a custom field thumbnail called $video_thumbnail. Thanks for your help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter knolan2

    (@knolan2)

    I changed part of the code to:

    <?php
      /* Set variables and create if stament */
      $video_thumbnail = get_post_meta($post->ID, 'Video Thumbnail');
      $videosite = get_post_meta($post->ID, 'Video Site', single);
      $videoid = get_post_meta($post->ID, "Video ID", single);
      if ($videosite == vimeo) {
      echo '<iframe src="https://player.vimeo.com/video/'.$videoid.'" width="300" height="200" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
      } else if ($videosite == youtube) {
      echo '<a class="fancybox"  title="" href="https://www.youtube.com/watch?v='.$videoid.'?fs=1&autoplay=1"><img src="' .$video_thumbnail. '" /></a>';
      } else {
      echo 'Please Select Video Site Via the CMS';
      }
    ?>

    and now I am getting the img src= array

    Moderator bcworkz

    (@bcworkz)

    Hello again knolan2,

    You need to pass true as a third parameter to get_post_meta() so that it returns a string instead of an array.
    $video_thumbnail = get_post_meta( $post->ID, 'Video Thumbnail', true );

    Thread Starter knolan2

    (@knolan2)

    I tried it wasn’t working. I changed the code to use the feature image as the thumbnail. If anyone wants to know what I did:

    <?php
                    /* Set variables and create if stament */
    				$thumb_id = get_post_thumbnail_id();
    				$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
    				$thumb_url = $thumb_url_array[0];
    				$videosite = get_post_meta($post->ID, 'Video Site', single);
    				$videoid = get_post_meta($post->ID, "Video ID", single);
    				if ($videosite == vimeo) {
      				echo '<a class="fancybox" title="" href="https://www.vimeo.com/'.$videoid.'"><span class="tv-title">'. get_the_title() .'</span><img src="' . $thumb_url . '" /></a>';
    				} else if ($videosite == youtube) {
      				echo '<a class="fancybox" title="" href="https://www.youtube.com/watch?v='.$videoid.'?fs=1&autoplay=1"><span class="tv-title">'. get_the_title() .'</span><img src="' . $thumb_url . '" /></a>';
    				} else {
      				echo 'Please Select Video Site Via the CMS';
    				}
    			?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Attach custom field thumbnail using php’ is closed to new replies.