WordPress.org

Forums

[resolved] PHP Code in Widget for Custom Field – default value (3 posts)

  1. Rob Cubbon
    Member
    Posted 3 years ago #

    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.

  2. vtxyzzy
    Member
    Posted 3 years ago #

    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';
    }
    ?>
  3. Rob Cubbon
    Member
    Posted 3 years ago #

    That works, vtxyzzy, I'm so grateful! Thank you so much!

Topic Closed

This topic has been closed to new replies.

About this Topic