• Hi, I wanted to display certain custom field if it has a value. I already acquire one method but I dont know how to do it in multiple lines. I am not php guy so I would be happy to get help here. Here is the code I got.

    <?php if ( get_post_meta($post->ID,'summary', true) ) { ?>
    <?php $values = get_post_custom_values("summary"); echo ' <strong>Summary:</strong> ' . $values[0]; ?>
    <?php } ?>

    Aside from summary I also wanted to included other lines like Recommendation and Conclusion. So the final values for EACH field would be something like this:

    Summary: Blah blah
    Recommendation: Blah blah
    Conclusion: Blah blah

    Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try

    function my_display_values()
    {
    	global $post;
    	$values = array(
    		'Summary' => get_post_meta( $post->ID, 'summary', true ),
    		'Recommendation' => get_post_meta( $post->ID, 'recommendation', true ),
    		'Conclusion' => get_post_meta( $post->ID, 'conclusion', true )
    	);
    	foreach( $values as $key => $value )
    	{
    		if ( !empty( $value ) ) { print $key.': '.$value.'<br>'; }
    	}
    }

    To call in your single post template use <?php echo my_display_values(); ?>

    You get the post id using the global $post , retrieve the custom post meta, iterate over the array called $values and print out the results only if the custom meta has content.

    Thread Starter bongkph

    (@bongkph)

    Thanks Nate, that works well.

    Good, glad to hear.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Field Values’ is closed to new replies.