Featured Video rather than Image for WordPress

Some templates have a featured image, but sometimes you want a video instead… But not all the time..

You can set some custom fields, and then add in PHP – if the field has something in it – display the embedded video (at the same size the featured image would have been…)

The text in the custom field should just be the ID code for YouTube or Vimeo (or you can add your own if you want… but you need to add the embed code to the PHP)

The code gets the ID and inserts it into the embed code.

<?php
if (post_custom(‘a YouTube ID Code’)) {
echo ‘<iframe width=”640″ height=”402″ src=”https://www.youtube.com/embed/’ . get_post_meta($post->ID, ‘a YouTube ID Code’, $single = true) . ‘” frameborder=”0″ allowfullscreen></iframe>’ ;
}
else if (post_custom(‘a Vimeo ID Code’)){
echo ‘<iframe src=”http://player.vimeo.com/video/’ . get_post_meta($post->ID, ‘a Vimeo ID Code’, $single = true) . ‘?title=0&amp;byline=0&amp;portrait=0″ width=”400″ height=”225″ frameborder=”0″ webkitAllowFullScreen allowFullScreen></iframe>’ ;
}else{
?>

 

add the YouTube or Vimeo ID code to a custom field:

  • a YouTube ID Code
  • a Vimeo ID Code

EG:

  • bH9tJXm2Bcw
  • 31934775

From the original YouTube links:

  • https://www.youtube.com/watch?=bH9tJXm2Bcw&feature=channel_video_title
  • http://vimeo.com/31934775