• Resolved Stefan

    (@stefan83)


    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)
  • <?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

    (@stefan83)

    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

    (@wordpressismypuppet)

    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:

    https://php.net/manual/en/function.empty.php

    $values = get_post_meta($post->ID, 'treatment_price', true);
    if ($values){
    echo '£ ';
    echo $values;
    echo ' per treatment ';
    }
    Thread Starter Stefan

    (@stefan83)

    Thanks YouON – works like a charm

    wordpressismypuppet – That returned an error: Fatal error: Can’t use function return value in write context in…

    wpismypuppet

    (@wordpressismypuppet)

    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.