Title: Conditional Meta Data &amp; Custom Fields
Last modified: August 19, 2016

---

# Conditional Meta Data & Custom Fields

 *  [mfal55](https://wordpress.org/support/users/mfal55/)
 * (@mfal55)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/conditional-meta-data-custom-fields/)
 * Hi there,
    I’m trying to set up some conditional code, where I’ve set up some
   custom fields and if they are used, I don’t want specific post meta data to display.
   The field I have set up is Byline, which is entered if the article isn’t by any
   of the authors of the system. This is the code I have so far. I can get the custom
   field to show up, but I can’t get the author to show up if no custom field is
   used. Can anyone tell me what I’m doing wrong?
 *     ```
       <?php
       if ($key="Byline") {
             get_post_meta($post->ID, $key, true);
          } else {
             echo 'the_author()';
          }
        ?>
   
       Thanks in advance for your help.
       ```
   

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

 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/conditional-meta-data-custom-fields/#post-1916142)
 * for one thing your code for the_author is wrong. I didn’t even look at the rest
   of it
 * it should be
 *     ```
       } else {
             the_author();
          }
       ```
   
 *  Thread Starter [mfal55](https://wordpress.org/support/users/mfal55/)
 * (@mfal55)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/conditional-meta-data-custom-fields/#post-1916174)
 * Thanks – I had it that way too, but it didn’t work. The code below manages to
   show the custom field when there is one used, but the else doesn’t show just 
   the author when there isn’t the custom field used. I feel like I’m missing something
   simple that I’m just not seeing.
 *     ```
       <?php
       if ($key="Byline") {
             echo get_post_meta($post->ID, $key, true);
          } else {
             the_author();
          }
        ?>
       ```
   
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/conditional-meta-data-custom-fields/#post-1916175)
 * The code you are using has to be run with the WordPress loop. Is that where you
   are running this?
 *  Thread Starter [mfal55](https://wordpress.org/support/users/mfal55/)
 * (@mfal55)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/conditional-meta-data-custom-fields/#post-1916177)
 * I’m adding this to the single.php page. I’m within the loop and am working on
   it just above <?php the_content(); ?>. I’m not getting any errors, the else just
   doesn’t show.
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/conditional-meta-data-custom-fields/#post-1916178)
 * for testing purposes, what happens if you put the_author() before the if statement?
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/conditional-meta-data-custom-fields/#post-1916180)
 * the `if ($key="Byline")` part gives the $key variable a value of “Byline” so 
   that is always true. To test if a post has a custom field “Byline” try it with
   this:
 *     ```
       <?php
       $key = get_post_meta($post->ID, 'Byline', true);
       if ($key != '') {
             echo $key;
          } else {
             the_author();
          }
       ?>
       ```
   
 *  Thread Starter [mfal55](https://wordpress.org/support/users/mfal55/)
 * (@mfal55)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/conditional-meta-data-custom-fields/#post-1916181)
 * Keesiemeijer – that’s exactly it! Works perfectly now.
 * Thanks to both of you for your help!
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/conditional-meta-data-custom-fields/#post-1916187)
 * Actually I see what the problem is
 * you have
    `if ($key="Byline") {` You are assigning the value Byline to $key every
   time the code runs
 * What you mean is compare the value of $key with the string ‘Byline’
    the first
   line should be `if ($key=="Byline") {`
 *  Thread Starter [mfal55](https://wordpress.org/support/users/mfal55/)
 * (@mfal55)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/conditional-meta-data-custom-fields/#post-1916189)
 * Thanks stvwlf! Keesiemeijer had the solution. I appreciate your help. 🙂

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

The topic ‘Conditional Meta Data & Custom Fields’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 3 participants
 * Last reply from: [mfal55](https://wordpress.org/support/users/mfal55/)
 * Last activity: [15 years, 3 months ago](https://wordpress.org/support/topic/conditional-meta-data-custom-fields/#post-1916189)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
