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