Title: Problem with if/else custom field
Last modified: August 20, 2016

---

# Problem with if/else custom field

 *  Resolved [Stefan](https://wordpress.org/support/users/stefan83/)
 * (@stefan83)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/problem-with-ifelse-custom-field/)
 * The following code works if I add a value to the custom field but when I delete
   the value, instead of showing nothing, I’m left with ‘From £’.
 *     ```
       <?php
       $values = get_post_custom_values("treatment_price");
       if ( is_array($values) ) {
       echo '<h3><strong>From £' . $values[0]. '</strong></h3>';
       }
       else {
        echo '';
       }
       ?>
       ```
   
 * What’s wrong?
 * Thanks

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/problem-with-ifelse-custom-field/#post-2529495)
 *     ```
       <?php
       if ( get_post_custom_values("treatment_price") ) :
       $values = get_post_custom_values("treatment_price");
       echo '<h3><strong>From £' . $values[0]. '</strong></h3>';
       else :
        echo '';
       endif;?>
       ```
   
 *  Thread Starter [Stefan](https://wordpress.org/support/users/stefan83/)
 * (@stefan83)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/problem-with-ifelse-custom-field/#post-2529703)
 * Thanks for the reply, Esmi. I’m afraid it still doesn’t work for some reason.
   To clarify, I’m using:
 *     ```
       <?php
       if ( get_post_custom_values("treatment_price") ) :
       $values = get_post_custom_values("treatment_price");
       echo '<strong>£' . $values[0]. ' per treatment</strong>';
       else :
        echo '';
       endif;?>
       ```
   
 * If I enter 10 as the value, i get ‘£10 per treatment’, but then if I delete the
   10 from the field, I’m left with ‘£ per treatment’.
 * Any ideas?
 *  [wpismypuppet](https://wordpress.org/support/users/wordpressismypuppet/)
 * (@wordpressismypuppet)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/problem-with-ifelse-custom-field/#post-2529712)
 * try:
 *     ```
       <?php
       if ( !empty (get_post_custom_values("treatment_price") ) ) :
       $values = get_post_custom_values("treatment_price");
       echo '<strong>£' . $values[0]. ' per treatment</strong>';
       else :
        echo '';
       endif;?>
       ```
   
 * Works on arrays too, if that’s what being returned:
 * [http://php.net/manual/en/function.empty.php](http://php.net/manual/en/function.empty.php)
 *  [Alessio Lo Vecchio](https://wordpress.org/support/users/youon/)
 * (@youon)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/problem-with-ifelse-custom-field/#post-2529713)
 *     ```
       $values = get_post_meta($post->ID, 'treatment_price', true);
       if ($values){
       echo '£ ';
       echo $values;
       echo ' per treatment ';
       }
       ```
   
 *  Thread Starter [Stefan](https://wordpress.org/support/users/stefan83/)
 * (@stefan83)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/problem-with-ifelse-custom-field/#post-2529720)
 * Thanks YouON – works like a charm
 * wordpressismypuppet – That returned an error: Fatal error: Can’t use function
   return value in write context in…
 *  [wpismypuppet](https://wordpress.org/support/users/wordpressismypuppet/)
 * (@wordpressismypuppet)
 * [14 years, 3 months ago](https://wordpress.org/support/topic/problem-with-ifelse-custom-field/#post-2529728)
 * Dang… over complicated it. It was a long, late night! Good catch YouON.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Problem with if/else custom field’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 4 participants
 * Last reply from: [wpismypuppet](https://wordpress.org/support/users/wordpressismypuppet/)
 * Last activity: [14 years, 3 months ago](https://wordpress.org/support/topic/problem-with-ifelse-custom-field/#post-2529728)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
