Title: Attach custom field thumbnail using php
Last modified: August 22, 2016

---

# Attach custom field thumbnail using php

 *  Resolved [knolan2](https://wordpress.org/support/users/knolan2/)
 * (@knolan2)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/attach-custom-field-thumbnail-using-php/)
 * 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="http://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="http://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](https://wordpress.org/support/users/knolan2/)
 * (@knolan2)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/attach-custom-field-thumbnail-using-php/#post-5692812)
 * 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="http://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="http://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](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/attach-custom-field-thumbnail-using-php/#post-5692989)
 * 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](https://wordpress.org/support/users/knolan2/)
 * (@knolan2)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/attach-custom-field-thumbnail-using-php/#post-5693042)
 * 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="http://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="http://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.

## Tags

 * [custom field](https://wordpress.org/support/topic-tag/custom-field/)
 * [get_post_meta](https://wordpress.org/support/topic-tag/get_post_meta/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [thumbnail](https://wordpress.org/support/topic-tag/thumbnail/)
 * [Youtube](https://wordpress.org/support/topic-tag/youtube/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 2 participants
 * Last reply from: [knolan2](https://wordpress.org/support/users/knolan2/)
 * Last activity: [11 years, 4 months ago](https://wordpress.org/support/topic/attach-custom-field-thumbnail-using-php/#post-5693042)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
