1

I need to replace featured images on pages with a video if certain posts contain a video from YouTube, Vimeo, other video hosting services allowed by WordPress.

Here's the pseudo code that I want to use on a custom page:

if ( has_post_thumbnail( get_the_ID() {
    if has_video(pseudocode to check whether the single post contains youtube video)  {
// show video player
    } else {
// show post thumbnail
    }
}

Any tips?

  • This depends on the output of your theme, you can always create a custom field that stores the video link and embed that if found in the output of your theme – Tunji Jan 2 '17 at 11:10
  • one tip would be to look at your single.php and see where it's displaying the thumbnail. You'll need to surround that with an if/then statement that says if there is a video (custom field) then show video instead of image. Without your code or attempts, there is no way to help out more. – rudtek Jan 2 '17 at 17:56
  • Not sure what is the question here. What is the actual problem you run into while trying to code the solution? – Mark Kaplun Jan 2 '17 at 18:27
  • @MarkKaplun I need to replace the featured image in my WordPress pages with a video player. Or automatically override any existing featured image with a video player if the single post contains an youtube video link. Am I enough clear now? Thanks! – Knott Jan 2 '17 at 18:50
2

Let's say for simplicity that you're using ACF. You need to create a video link field once ACF is installed and assign it to posts. Then, in the post you want a video to show up, add the url. I usually assign this field as a text field and then have users enter the youtube id (last string of text after the youtube.com link.

For example, if the video link was: https://www.youtube.com/J-ek8drxFJA

the user would enter just J-ek8drxFJA in the field.

Now add this code to your single.php copy that is in your child theme folder

Notice that I changed the if statement. No need to look for the thumbnail if you're going to use a video:

if( get_field( 'video_link' ) ) {
    echo 'this is my video link id ' . get_field( 'video_link' );   // or show video player (see below)
} else {
    if( has_post_thumbnail( get_the_ID() {
        // show post thumbnail
    }

    //no video or photo
}

Now of course if you want to show the video you'll want to do something like this instead:

if( get_field( 'video_link' ) ) {
    $videoid = get_field( 'video_link' );
    echo '<h2>Video Link</h2><p>see our video:</p><iframe width="420" height="315" src="https://www.youtube.com/embed/' . $videoid . '" frameborder="0" allowfullscreen></iframe>';
} else {
    if( has_post_thumbnail( get_the_ID() ) ) {
        // show post thumbnail
    }

    //no video or photo
}

Obviously you can change the code up a bit to suit your needs.

  • 2
    While quality answers of any kind are acceptable do please note that answers referring the user to 3rd party plugins are sometimes not well received. These type of answers have a tendency to generate additional questions regarding forementioned 3rd party plugin and those type of questions are Off Topic. – Howdy_McGee Jan 2 '17 at 20:40
1

I think you should give a try to this plugin : https://wordpress.org/plugins/featured-video-plus/

If you don't like to use extra plugin then you should definitely look into their code for reference. you can look into their code and customize them as per your requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.