In our previous post, we explained how to use easily either Featured Image or placeholder instead. Same method may be applied to YouTube video. If site owner prefers to alternate between either picture or video it is achieved in same easy way with conditional tag. On the screenshot above you see YouTube video, but for another post a website owner may use just Featured Image.
So, here we go:
<?php if (has_post_thumbnail()) : ?>
<?php the_post_thumbnail('full'); ?>
<?php else : ?>
$media = get_post_meta($post->ID, 'media', true);
echo '<div class="video-container">' . '<div class="video-holder">'. $media . '</div>' . '</div>';
<?php endif; ?>
In the code above (which we naturally, add to inside the loop) we see two parts: first is responsible for Featured Image while second one dispalys YouTube video. In the first part everything is simple. As for the second part there are few notes.
First: we use custom field in order to insert video individual only for this very post. We defined a variable $media (and its title may be any) and added conditional which could be translated into simple language as: if we use custom field with title media, let’s insert video URL inside the custom field. Second: we also defined post-meta, in other words— a custom filed. What is left is just to create a new custom field, entitle it “media” and insert video URL into “value” input. Here, how it looks from the backend: