Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

IF statement for a custom field WordPress

  • SOLVED

I want to have a video appear if a field is filled out but I want the video to disappear if the field is empty. Is there an IF statement I can use to hide this blank field? I had something like this, but it broke the site:

<?php
if (isset(meta(video_url)))
<p><iframe src="<?php meta(video_url);?>" width="570" height="320" frameborder="0"></iframe></p>
?>


with video:
[[LINK href="http://ec2-50-16-38-139.compute-1.amazonaws.com/wordpress/2011/03/category1/test-lou/"]]http://ec2-50-16-38-139.compute-1.amazonaws.com/wordpress/2011/03/category1/test-lou/[[/LINK]]

without:
[[LINK href="http://ec2-50-16-38-139.compute-1.amazonaws.com/wordpress/2011/03/category2/this-is-a-test-post/"]]http://ec2-50-16-38-139.compute-1.amazonaws.com/wordpress/2011/03/category2/this-is-a-test-post/[[/LINK]]

Answers (5)

2011-03-24

Nilesh shiragave answers:

Use this


<?php if (get_post_meta($post->ID, 'video_url', true)): ?>
<?php
$video = get_post_meta($post->ID, 'video_url', true);
?>
<p><iframe src="<?php echo $video;?>" width="570" height="320" frameborder="0"></iframe></p>
<?php endif; ?>

2011-03-23

Pippin Williamson answers:

Obviously replace "meta" with the actual custom fields function.


<?php

if (meta(video_url) != "" && meta(video_url) != null) { ?>

<p><iframe src="<?php meta(video_url);?>" width="570" height="320" frameborder="0"></iframe></p>
<?php } ?>


Louis Gubitosi comments:

This was the closest fix so far but its showing the URL and not the video in the iframe.

with video:
http://ec2-50-16-38-139.compute-1.amazonaws.com/wordpress/2011/03/category1/test-lou/

without:
http://ec2-50-16-38-139.compute-1.amazonaws.com/wordpress/2011/03/category2/this-is-a-test-post/


Pippin Williamson comments:

Rather than echoing the video url into the iframe, maybe try just pasting the entire iframe into the custom field instead.


Louis Gubitosi comments:

I'd rather just have the URL and leave the embed code in the template. Is there a way to do that?


Pippin Williamson comments:

Yes, the way that I posted should work for that. Can you paste your exact code, please?

2011-03-23

AdamGold answers:

if( $_POST['yourfield'] != "" ) {
?>
<p><iframe src="<?php echo $_POST['yourfield']; ?>" width="570" height="320" frameborder="0"></iframe></p>
<?php
}


AdamGold comments:

Ahh didn't notice you're referring to custom field. In this case, you will have to paste this code <strong>inside a loop</strong>:

if( get_post_meta('video_url') != "" ) {
?>
<p><iframe src="<?php get_post_meta('video_url');?>" width="570" height="320" frameborder="0"></iframe></p>
<?php
}

2011-03-23

David Navarrete answers:


<?php if ($video = get_post_meta(get_the_ID(), 'video_url', true)): ?>
<p><iframe src="<?php echo $video;?>" width="570" height="320" frameborder="0"></iframe></p>
<?php endif; ?>


you can use http://www.viper007bond.com/wordpress-plugins/vipers-video-quicktags/ and put the video on cfield then call the c-field and use apply filters. like this

<?php echo apply_filters('the_content', $video); ?>

2011-03-23

Sébastien | French WordpressDesigner answers:

<?php $video=get_post_meta(get_the_ID(), 'video', true);

if($video) { ?>
<p><iframe src="<?php echo $video;?>" width="570" height="320" frameborder="0"></iframe></p>
<?php } ?>