Forums

[resolved] Custom Field Value only if populated (3 posts)

  1. malawimama
    Member
    Posted 1 year ago #

    I need to show a custom field in posts, with some text before it, but I don't want to show the text if the custom field is empty. I tried some code from wprecipes but it was showing the text before it even when the field wasn't added, then I checked the Docs but it was too confusing because it doesn't mention how to add echo 'some text here' before the field values.

    I need to somehow detect if the field is added and only show that text if it's added to the post, and if not, then hide that text.

    For instance, I want to add "First published in Some Publication Name Jan 2010"

    I wanted to create a custom field value key named 'publication' where the text 'First Published in' is automatically populated so the admin doesn't have to add that to the value box. I was thinking of adding echo 'First published in'; to the code in the Loop somehow.

    Does anyone know of any tutorials or code that I can use in the latest version of WP?

    Thanks!~

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    <?php if ( get_post_meta($post->ID, 'publication', true) ) : ?>
    <p>Some text here: <?php echo get_post_meta($post->ID, 'publication', true) ?></p>
    <?php endif; ?>

    http://codex.wordpress.org/Function_Reference/get_post_meta

  3. malawimama
    Member
    Posted 1 year ago #

    PERFECT!!! WORKS GREAT! Thanks esmi you're awesome!

Topic Closed

This topic has been closed to new replies.

About this Topic