Support » Fixing WordPress » PHP Code in Widget for Custom Field – default value

  • Resolved Rob Cubbon

    (@robcub)


    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)
  • 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';
    }
    ?>

    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.