Title: PHP Code in Widget for Custom Field – default value
Last modified: August 20, 2016

---

# PHP Code in Widget for Custom Field – default value

 *  Resolved [Rob Cubbon](https://wordpress.org/support/users/robcub/)
 * (@robcub)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/php-code-in-widget-for-custom-field-default-value/)
 * Hello,
 * I’m using the plugin PHP Code Widget to display a custom field in the sidebar
   by every page.
 *     ```
       <?php global $wp_query;
       if(is_object($wp_query->queried_object) && $wp_query->queried_object->ID)
       {echo get_post_meta($wp_query->queried_object->ID, 'sidebar', true);}
       ?>
       ```
   
 * This returns the value of the key Custom Field called ‘sidebar”.
 * I would like it to return a default echo (for example, “Well, hello!”) if no 
   value for this Custom Field key has been entered.
 * I’ve tried a few things but i keep on getting PHP errors.

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

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/php-code-in-widget-for-custom-field-default-value/#post-2449629)
 * Give this a try:
 *     ```
       <?php global $wp_query;
       if(is_object($wp_query->queried_object) && $wp_query->queried_object->ID)
       {
          $sidebar = get_post_meta($wp_query->queried_object->ID, 'sidebar', true);
          echo ($sidebar) ? $sidebar : 'default message';
       }
       ?>
       ```
   
 *  Thread Starter [Rob Cubbon](https://wordpress.org/support/users/robcub/)
 * (@robcub)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/php-code-in-widget-for-custom-field-default-value/#post-2449635)
 * That works, vtxyzzy, I’m so grateful! Thank you so much!

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

The topic ‘PHP Code in Widget for Custom Field – default value’ is closed to new
replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [Rob Cubbon](https://wordpress.org/support/users/robcub/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/php-code-in-widget-for-custom-field-default-value/#post-2449635)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
