Do not render code when Custom Field has no value [image URL]

Discussion in 'WordPress' started by radiant_luv, May 16, 2011.

  1. #1
    It must be easy for a wordpress developer/ Php guy and I am not a Php guy. Please point me to the right direction.

    I have the below code in single.php

    
    <img src="<?php $values = get_post_custom_values("thumbs"); echo $values[0]; ?>" width="250" height="180" class="thumb-new" alt="" />
    
    Code (markup):
    when I add an image URL using custom field it shows the image. Now that image has Border around using CSS. And when there is no image URL mention it still renders the code <img src'" ">

    I want when there is no value (img URL) mentioned in Custom Field it shouldn't render the <img src> Code.

    Thanks in advance.
     
    radiant_luv, May 16, 2011 IP
  2. radiant_luv

    radiant_luv Peon

    Messages:
    1,327
    Likes Received:
    34
    Best Answers:
    1
    Trophy Points:
    0
    #2
    12 hours over, 36 views and no responce yet. So this is not as easy as I thought?
     
    radiant_luv, May 16, 2011 IP
  3. IAreOwn

    IAreOwn Active Member

    Messages:
    46
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    95
    #3
    try this
    
    <?php
    
    $values = get_post_custom_values("thumbs"); 
    
    if(!empty($values[0])) { ?>
    <img src="<?php echo $values[0]; ?>" width="250" height="180" class="thumb-new" alt="" />
    
    <?php } ?>
    
    PHP:
     
    IAreOwn, May 16, 2011 IP
    radiant_luv likes this.
  4. radiant_luv

    radiant_luv Peon

    Messages:
    1,327
    Likes Received:
    34
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Works like a charm. Thanks IAreOwn. +Rep to you :)
     
    radiant_luv, May 17, 2011 IP