I’m using a custom field to display a video using the shortcode in my single page template:
<?php
// Get the video URL and put it in the $video variable
$videoID = get_post_meta($post->ID, 'youtube_url', true);
// Check if there is in fact a video URL
if ($videoID) {
echo '<div class="videoContainer">';
// Echo the embed code via oEmbed
echo do_shortcode('[fve]http://youtu.be/' . $videoID . '[/fve]' );
echo '</div>';
}
?>
The “youtube_url” is what I put as the custom field and then I just put the youtube video ID/code (the alphanumeric part of the video url) whatever you want to call it as the variable.
For example http://www.youtube.com/watch?v=nGvOMUZrqVM I’d just put nGvOMUZrqVM in. You can obviously edit the code as you wish. I’m going to tinker with it in a bit to see if I can just put the whole url in so it should also work with vimeo. Don’t know if you ever worked it out but I was looking for this so thought I’d share if others got stuck.
So I did some fiddling and I think the following code not only lets you use vimeo as well as youtube but is probably more simple for users as well as they can just put the whole url in the box.
<?php
// Get the video URL and put it in the $video variable
$videoID = get_post_meta($post->ID, 'video_url', true);
// Check if there is in fact a video URL
if ($videoID) {
echo '<div class="videoContainer">';
// Echo the embed code via oEmbed
echo do_shortcode('[fve]' . $videoID . '[/fve]' );
echo '</div>';
}
?>
I changed “youtube_url” to “video_url” as the custom field name. You can use pretty much whatever name you want though, as long as it matches your code.