Support » Fixing WordPress » if statement for get_post_meta() or how to display custom field if it exists

Viewing 2 replies - 1 through 2 (of 2 total)
  • Try this…

    //Custom Fields 
    
    function blog_short_info(){
    global $post;
    return get_post_meta($post->ID, 'Blog_Post_Short_Info', true);
    }
    add_action('the_content','blog_short_info');
    <?php if ( blog_short_info() ) { ?>
    	<div class="blog-short-info">
    		<?php echo blog_short_info();?>
    	</div>
    <?php } ?>

    @rajesh Soni Thank you very much for your help! It worked like a charm, but may I ask you why I needed to use return in the blog_short_info() function instead of echo and use the echo when I was calling the function inside the if statement?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘if statement for get_post_meta() or how to display custom field if it exists’ is closed to new replies.