Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How would I adjust the following code to only show the image tag when there is a variable present in the "imageembed" field?

<?php $imageid = get_post_meta($post->ID, 'imageembed', true); ?>
   <div class="newsimage"><img src="<?php echo wp_get_attachment_url( $imageid ); ?>" width="640" /></div>
share|improve this question
add comment

3 Answers

<?php
    $imageid = get_post_meta($post->ID, 'imageembed', true);
    if($imageid != '') 
       { ?>
        <div class="newsimage"><img src="<?php echo wp_get_attachment_url( $imageid ); ?>" width="640" /></div>
<?php  } ?>

Use if condition to check if $imageid has any value in it or not.

share|improve this answer
add comment
up vote 0 down vote accepted
    <?php    
  if (strlen($imageid = get_post_meta($post->ID, 'imageembed', true))) {
?>
    <div class="newsimage"><img src="<?php echo wp_get_attachment_url((int) $imageid); ?>" width="640" /></div>
<?php } ?>
share|improve this answer
add comment

Simple. Use the if conditional statement, like this:

<?php
    if ( get_post_meta($post->ID, 'imageembed', true) ) {
        $imageid = get_post_meta($post->ID, 'imageembed', true);
?>

    // Show when custom field has a value
    <div class="newsimage"><img src="<?php echo wp_get_attachment_url( $imageid ); ?>" width="640" /></div>

<?php } ?>

READ: Function Reference / get_post_meta (WordPress Codex)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.