Forums

Only 'echo' if custom fields exist (2 posts)

  1. malawimama
    Member
    Posted 1 year ago #

    I'm having trouble creating 2 custom fields. I tried looking at get_post_custom() but I got in way over my head, because it was breaking the code somehow.

    What I need is to add a Publication date and the publication name (like a magazine for instance) so it looks like this:

    Published: 03/12/2010 in Suchandsuch Magazine

    The code I have currently works, but it shows the "Published:" and "in" text even if there's no custom field added, so I need to figure out how to hide that text if there is no custom fields added.

    Here's my current code.

    Functions.php file:

    function get_custom_field_value($szKey, $bPrint = false) {
    	global $post;
    	$szValue = get_post_meta($post->ID, $szKey, true);
    	if ( $bPrint == false ) return $szValue; else echo $szValue;
    }

    In the loop:

    <?php if ( function_exists('get_custom_field_value') ){
            echo 'Original Publication: ';
            get_custom_field_value('publication_date', true);
    } ?> <?php if ( function_exists('get_custom_field_value') ){
            echo 'in ';
            get_custom_field_value('publication', true);
    } ?>

    Does anyone know how to hide the echo text if there's no custom fields there?

    Thanks!~

  2. malawimama
    Member
    Posted 1 year ago #

    Actually, that code above is not working properly :(

    I saw a post with a link to a PDF file and the text after it was gone, and the link text itself was replaced with "Original Publication" ...
    Weird, I know. And the link was in the wysiwyg, not in a custom field.

    So back to square one...

Topic Closed

This topic has been closed to new replies.

About this Topic