0

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.

<?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...

1
  • You have php tags inside your first echo statement. Should be echo '<img src="http://img.youtube.com/vi/' . get_post_meta($post->ID,'video_url',true) .'/0.jpg"/>';
    – cjbj
    Jun 5 '16 at 13:56
1

There are three problems

1. echo and php tags within an echo

Change your first echo from:

 echo '<img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID,'video_url',true);?>/0.jpg"/>';

to this:

 echo '<img src="http://img.youtube.com/vi/' . get_post_meta($post->ID,'video_url',true) . '/0.jpg"/>';

2. if statement not wrapped and no brackets

change:

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

to:

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

and add {} around that code block:

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
}

3. else instead of elseif

the first else should be an elseif. so change:

else ( has_post_thumbnail() ) {

to:

elseif ( has_post_thumbnail() ) {

Hope that is all of them. In the future try using a tool such as PHP Code Checker to check your code.

7
  • Thanks. I changed the code but the webpage is displaying blank. Must be something else that is not working...
    – Arete
    Jun 5 '16 at 19:49
  • Turn wp debug on to track down the problem.
    – yobddigi
    Jun 5 '16 at 20:47
  • I am not able to use wp debug. It is a live site.
    – Arete
    Jun 6 '16 at 19:45
  • If you can't turn debug in then I wouldn't suggest testing code on it ether.
    – yobddigi
    Jun 6 '16 at 19:48
  • 1
    I found the problems. I updated my answer to address them. Hope that is all of them :)
    – yobddigi
    Jun 6 '16 at 20:08

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.