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.
CodePudding user response:
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.';
}
CodePudding user response:
It seems like you are using the <?php tag in a way that you are cutting the if else condition. Try this.
<?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{
echo( 'No Video Here' );
}
?>