Title: style individual custom fields
Last modified: August 19, 2016

---

# style individual custom fields

 *  [twinpine](https://wordpress.org/support/users/twinpine/)
 * (@twinpine)
 * [17 years ago](https://wordpress.org/support/topic/style-individual-custom-fields/)
 * Ok, so I have finally managed to display my individual custom fields on a page
   by using this code:
 *     ```
       <div id="bday" style="background-color: red; padding: 20px;">
       <?php $custom_field = get_post_meta($post->ID, 'respect', true);
                if($custom_field !== '') {
       	echo $custom_field;
       	} ?>
       </div>
       ```
   
 * My question is how would I code this so if there is no value entered for ‘birthday’
   in a post an empty div box would not be displayed?
    I’ve tried putting the div
   tags in the if statement but I just get an error message when I try and display
   my page.
 * Many thanks.
 * P.S. Go easy on me I know next to nothing about PHP!

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

 *  Thread Starter [twinpine](https://wordpress.org/support/users/twinpine/)
 * (@twinpine)
 * [17 years ago](https://wordpress.org/support/topic/style-individual-custom-fields/#post-1037763)
 * Sorry that post may not have made much sense. I have put ‘respect’ as the key
   for some reason when I meant to put ‘birthday’ so the code will actually look
   like this:
 *     ```
       <div id="bday" style="background-color: red; padding: 20px;">
       <?php $custom_field = get_post_meta($post->ID, 'birthday', true);
        if($custom_field !== '') {
       	echo $custom_field;
       	} ?>
       </div>
       ```
   
 *  [t31os](https://wordpress.org/support/users/t31os/)
 * (@t31os)
 * [17 years ago](https://wordpress.org/support/topic/style-individual-custom-fields/#post-1037773)
 * Try like this…
 *     ```
       <?php $custom_field = get_post_meta($post->ID, 'birthday', true); if($custom_field) : ?>
       <div id="bday" style="background-color: red; padding: 20px;">
       <?php echo $custom_field;?>
       </div>
       <?php endif;?>
       ```
   
 *  Thread Starter [twinpine](https://wordpress.org/support/users/twinpine/)
 * (@twinpine)
 * [17 years ago](https://wordpress.org/support/topic/style-individual-custom-fields/#post-1037780)
 * Awesome, that has done the trick! Thanks very much for your help t31os, you were
   right on the money.
 *  [t31os](https://wordpress.org/support/users/t31os/)
 * (@t31os)
 * [17 years ago](https://wordpress.org/support/topic/style-individual-custom-fields/#post-1037782)
 * Of course this is the same as doing…
 *     ```
       <?php $custom_field = get_post_meta($post->ID, 'birthday', true); if($custom_field) { ?>
       <div id="bday" style="background-color: red; padding: 20px;">
       <?php echo $custom_field;?>
       </div>
       <?php } ?>
       ```
   
 * Depends if you prefer
    `:` and `endif;` or `{` and `}`
 * Just to be clear, the syntax is valid in both…. I thought the first example just
   looked easier to read …
 * Your initial mistake was placing the DIV outside the IF check. Also the `if($
   var !== '')` only checks if the field is not equal to `''` (or nothing), which
   is not quite the same as checking if it’s empty/exists…
 * Glad i could help.. 🙂
 *  Thread Starter [twinpine](https://wordpress.org/support/users/twinpine/)
 * (@twinpine)
 * [17 years ago](https://wordpress.org/support/topic/style-individual-custom-fields/#post-1037790)
 * Hey thanks for taking the time to explain it all to me t31os, it’s all gonna 
   be really helpful to know for my project.
 *  [t31os](https://wordpress.org/support/users/t31os/)
 * (@t31os)
 * [17 years ago](https://wordpress.org/support/topic/style-individual-custom-fields/#post-1037859)
 * I think applies to most cases in PHP to…
 *     ```
       whie(something) {
   
       }
       ```
   
 * or
 *     ```
       while(something) :
   
       endwhile;
       ```
   
 *     ```
       if(something) {
   
       } else {
   
       something else
   
       }
       ```
   
 * or
 *     ```
       if(something) :
   
        else :
   
       something else
   
       endif;
       ```
   
 *     ```
       foreach(something) {
   
       }
       ```
   
 * or
 *     ```
       foreach(something) :
   
       endforeach;
       ```
   
 * Each coder uses a different method, or may combine both… I never quite realised
   they were the same until a little while back.. which has totally opened up my
   understanding of PHP…
 * Most recent enjoyable functions to learn were explode and implode…
 * I’m still hurting my head with the RegEx stuff (regular expressions), my god 
   is that overwhelming…. once you start mixing in multiple operators [^(?*.) etc…
 * I think once you start using a few functions other will make sense and so on…
   then you’ll start to grasp how flexible PHP is… it’s good fun i tell ya!! 🙂
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [17 years ago](https://wordpress.org/support/topic/style-individual-custom-fields/#post-1037928)
 * You forgot `switch` statements… 😉
 * I know what you mean about regex, though. Have you tried the [Regular Expression Library](http://www.regexlib.com/)?
   I’ve picked up some useful snippets from there. Plus there’s an online regexp
   tester that’s saved me hours of painful testing before now.
 *  [t31os](https://wordpress.org/support/users/t31os/)
 * (@t31os)
 * [17 years ago](https://wordpress.org/support/topic/style-individual-custom-fields/#post-1037945)
 * Thanks for the link, hadn’t seen that site before, having a read through some
   bits now… 🙂 Looks dead handy!! 🙂

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

The topic ‘style individual custom fields’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 3 participants
 * Last reply from: [t31os](https://wordpress.org/support/users/t31os/)
 * Last activity: [17 years ago](https://wordpress.org/support/topic/style-individual-custom-fields/#post-1037945)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
