Conditional custom field. Issues with code

This code should get a value from the custom field and show text based on that value. No matter what i do it seems to pop down to the else and show “No data”. I appreciate help locating the issue with this code.

(The custom field value changes from 0 to 9)

<?php $kpcustomfield = get_post_meta($post->ID, 'kp_custom_field', true);
         if($value == '0') {
              echo 'Tekst her KP0 tekst';
         } elseif($value == '1') {
              echo 'Tekst her KP1 tekst';
         } elseif($value == '2') {
              echo 'Tekst her KP2 tekst';
         } elseif($value == '3') {
              echo 'Tekst her KP3 tekst';
         } elseif($value == '4') {
              echo 'Tekst her KP4 tekst';
         } elseif($value == '5') {
              echo 'Tekst her KP5 tekst';
         } elseif($value == '6') {
              echo 'Tekst her KP6 tekst';
         } elseif($value == '7') {
              echo 'Tekst her KP7 tekst';
         } elseif($value == '8') {
              echo 'Tekst her KP8 tekst';
         } elseif($value == '9') {
              echo 'Tekst her KP9 tekst';
         } else {
              echo 'No data';
    }
?>

You are looking up the post id. So, if it fails, what is actually being returned? It may be you are looking in the wrong spot. Where do you assign anything to $value? Shouldn’t that be $kpcustomfield?

Also, your code is very redundant.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service