Failed to load in else condition -- [Question Asked]

Issue

I need to embedd video to my loop with condition. I try this code:

 <?php if (function_exists("wp_oembed_get")) {
 $url = esc_url( get_post_meta( get_the_ID(), 'wiki_test_embed', 1 ) );
 echo wp_oembed_get( $url );
 } else{    ?>
    No Video Here
 <?php
 }
 ?>

Everything work fine when videpinserted to the post, but when the post have no video, the text “No Video Here” doesnt appear.

Really appreciate for any helps.

Answer we found from sources

Your code is checking whether the function, wp_oembed_get(), exists. That should be true regardless of whether your post contains a video or not. You’ll never hit the else statement.

Instead of checking for the existence of a function, you need to be evaluating the value of your meta field. Example:

if ( $video_url = get_post_meta( get_the_ID(), 'wiki_test_embed', true ) ) {
    echo wp_oembed_get( esc_url( $video_url ) );
} else {
    echo 'No video was found.';
}

Answered By – Nathan Dawson

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Posted in PHP

Who we are?

We are team of software engineers in multiple domains like Programming and coding, Fundamentals of computer science, Design and architecture, Algorithms and data structures, Information analysis, Debugging software and Testing software. We are working on Systems developer and application developer. We are curious, methodical, rational, analytical, and logical. Some of us are also conventional, meaning we're conscientious and conservative.

Answer collected from stackoverflow and other sources, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0