Conditional featured image with youtube thumbnail in WordPress

Basically what I am trying to do here is: Display the youtube thumbnail. If no youtube thumbnail -> Display the Featured image for the post. If no featured image -> Display the fallback image.

However it seems I am doing something wrong because the webpage displays blank.

Read More
<?php
            // Check if the post has a Youtube thumbnail (using custom field video_url)
    if get_post_meta($post->ID,'video_url',true)
        echo '<img src="http://img.youtube.com/vi/' . get_post_meta($post->ID,'video_url',true) . '/0.jpg"/>';
            // Check if the post has a Post Thumbnail assigned to it
    else ( has_post_thumbnail() ) {
        echo '<a href="' . get_permalink($post->ID) . '" >';
        the_post_thumbnail('frontpage-thumb');
        echo '</a>';
            // If the post does not have a featured image then display the fallback image
    } else {
        echo '<a href="' . get_permalink($post->ID) . '" ><img src="'. get_stylesheet_directory_uri() . '/img/fallback-featured-image-index.jpg" /></a>';}
    ?>

The general code for displaying the youtube thumbnail with custom fields is <img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID,'video_url',true);?>/0.jpg"/> I just can’t get it to work with the conditional statements…

Related posts

Leave a Reply

1 comment

  1. In your first if condition, you have wrong syntax and the PHP must be closed

    if (get_post_meta($post->ID,'video_url',true)) // 
    

    Correct code

    <?php
    
        if (get_post_meta($post->ID,'video_url',true)) ?>
            echo '<img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID,'video_url',true);?>/0.jpg"/>';
    
        // Check if the post has a Post Thumbnail assigned to it
        else ( has_post_thumbnail() ) {
            echo '<a href="' . get_permalink($post->ID) . '" >';
            the_post_thumbnail('frontpage-thumb');
            echo '</a>';
    
        // If the post does not have a featured image then display the fallback image
        } else {
            echo '<a href="' . get_permalink($post->ID) . '" ><img src="'. get_stylesheet_directory_uri() . '/img/fallback-featured-image-index.jpg" /></a>';}
    ?>