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!
-
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
Hello again knolan2,
You need to pass
trueas a third parameter toget_post_meta()so that it returns a string instead of an array.
$video_thumbnail = get_post_meta( $post->ID, 'Video Thumbnail', true );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'; } ?>
The topic ‘Attach custom field thumbnail using php’ is closed to new replies.