Title: php if, else &amp; custom fields &#8211; syntax error
Last modified: August 30, 2016

---

# php if, else & custom fields – syntax error

 *  Resolved [abcdefghijklmnop](https://wordpress.org/support/users/abcdefghijklmnop/)
 * (@abcdefghijklmnop)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/php-if-else-custom-fields/)
 * Hi all,
 * I’m just starting to use custom fields and would like to use it to display an
   author box with (1) author photo (2) author name (3) author bio.
 * I use this code in loop-single.php and it works fine:
 *     ```
       <?php if ( get_post_meta($post->ID, 'author-photo', 'author-name', 'author-bio', true)     ) : ?>
       <div class="authorbox">
           <div class="authorphoto">
           <img src="<?php $key="author-photo"; echo get_post_meta($post->ID, $key, true); ?>"     />
           </div>
               <div class="authorbox-content">
                   <h3 class="author-name"><?php $key="author-name"; echo get_post_meta($post->ID, $key, true); ?></h3>
                   <p><?php $key="author-bio"; echo get_post_meta($post->ID, $key, true); ?></p>
               </div>
       </div>
       <?php endif; ?>
       ```
   
 * Basically, the above displays author photo, name and bio if all 3 custom field
   values exist.
 * Next, I tried to make it display only the author name and bio if the photo value
   does not exist. So, I add some code right below the above code and the resulting
   entire code looks like this:
 *     ```
       <?php if ( get_post_meta($post->ID, 'author-photo', 'author-name', 'author-bio', true)     ) : ?>
       <div class="authorbox">
           <div class="authorphoto">
           <img src="<?php $key="author-photo"; echo get_post_meta($post->ID, $key, true); ?>"     />
           </div>
               <div class="authorbox-content">
                   <h3 class="author-name"><?php $key="author-name"; echo get_post_meta($post->ID, $key, true); ?></h3>
                   <p><?php $key="author-bio"; echo get_post_meta($post->ID, $key, true); ?></p>
               </div>
       </div>
       <?php endif; ?>
   
       <?php if ( get_post_meta($post->ID, 'author-name', 'author-bio', true) ) : ?>
       <div class="authorbox">
               <div class="authorbox-content-nophoto">
                   <h3 class="author-name"><?php $key="author-name"; echo get_post_meta($post->ID, $key, true); ?></h3>
                   <p><?php $key="author-bio"; echo get_post_meta($post->ID, $key, true); ?></p>
               </div>
       </div>
       <?php endif; ?>
       ```
   
 * Now, this works fine if only the author name and bio values exist. However, if
   all 3 values exist i.e. photo, name and bio, then a problem arises… there will
   be 2 author boxes displayed (one with photo and one without photo).
 * How may I solve this problem? (I only need one author box displayed at any one
   time, either with photo or without photo).
 * Thanks in advance!
 * PS: I have tried adding ‘else’ but am not sure of the exact way to code it, I’ve
   received syntax errors when trying to add ‘else’ in between the code. Any pointers
   would be greatly appreciated!

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

 *  [jitendrajoshi](https://wordpress.org/support/users/jitendrajoshi/)
 * (@jitendrajoshi)
 * [10 years, 9 months ago](https://wordpress.org/support/topic/php-if-else-custom-fields/#post-6491630)
 * Can you try the following:
 *     ```
       <?php if ( get_post_meta($post->ID, 'author-photo', 'author-name', 'author-bio', true)     ) : ?>
       <div class="authorbox">
           <div class="authorphoto">
           <img src="<?php $key="author-photo"; echo get_post_meta($post->ID, $key, true); ?>"     />
           </div>
               <div class="authorbox-content">
                   <h3 class="author-name"><?php $key="author-name"; echo get_post_meta($post->ID, $key, true); ?></h3>
                   <p><?php $key="author-bio"; echo get_post_meta($post->ID, $key, true); ?></p>
               </div>
       </div>
   
       <?php elseif ( get_post_meta($post->ID, 'author-name', 'author-bio', true) ) : ?>
       <div class="authorbox">
               <div class="authorbox-content-nophoto">
                   <h3 class="author-name"><?php $key="author-name"; echo get_post_meta($post->ID, $key, true); ?></h3>
                   <p><?php $key="author-bio"; echo get_post_meta($post->ID, $key, true); ?></p>
               </div>
       </div>
       <?php endif; ?>
       ```
   
 * docs: [http://php.net/manual/en/control-structures.alternative-syntax.php](http://php.net/manual/en/control-structures.alternative-syntax.php)
 *  Thread Starter [abcdefghijklmnop](https://wordpress.org/support/users/abcdefghijklmnop/)
 * (@abcdefghijklmnop)
 * [10 years, 8 months ago](https://wordpress.org/support/topic/php-if-else-custom-fields/#post-6491779)
 * Hi @jitdendrajoshi .. thanks for taking the time to reply. In the end, i found
   the answer, the below code worked for me…
 *     ```
       <?php if ( get_post_meta($post->ID, 'author-photo', 'author-name', 'author-bio', true)     ) : ?>
       <div class="authorbox">
           <div class="authorphoto">
           <img src="<?php $key="author-photo"; echo get_post_meta($post->ID, $key, true); ?>"     />
           </div>
               <div class="authorbox-content">
                   <h3 class="author-name"><?php $key="author-name"; echo get_post_meta($post->ID, $key, true); ?></h3>
                   <p><?php $key="author-bio"; echo get_post_meta($post->ID, $key, true); ?></p>
               </div>
       </div>
       <?php endif; ?>
   
       <?php } else { ?>
   
       <?php if ( get_post_meta($post->ID, 'author-name', 'author-bio', true) ) : ?>
       <div class="authorbox">
               <div class="authorbox-content-nophoto">
                   <h3 class="author-name"><?php $key="author-name"; echo get_post_meta($post->ID, $key, true); ?></h3>
                   <p><?php $key="author-bio"; echo get_post_meta($post->ID, $key, true); ?></p>
               </div>
       </div>
       <?php endif; ?>
       ```
   

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

The topic ‘php if, else & custom fields – syntax error’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [abcdefghijklmnop](https://wordpress.org/support/users/abcdefghijklmnop/)
 * Last activity: [10 years, 8 months ago](https://wordpress.org/support/topic/php-if-else-custom-fields/#post-6491779)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
