March 16, 201214 yr Hi everyone, I'm wondering if anyone could possibly help me out with the following. I'm using Gravity Forms (Wordpress plugin) to create a front end form that visitors can fill in and submit, which then creates a "post" on the site. The following code is what I use to pull in the data that is submitted by the author. <p><strong>Label:</strong> <?php echo get_post_meta($post->ID, 'fieldname', true); ?></p> However, not all of the form fields are compulsory, so if a user were to leave one empty, the post template renders the following: Label: I'm looking for a way to say "if the field is empty, don't show anything within this p tag. I've used a similar technique in Joomla before, but don't really understand the concept well enough to write it myself for Wordpress. If anyone knows of any good tutorials to achieve this, or could give me some tips on the correct code, I'd really appreciate it. Thanks! Chris
March 16, 201214 yr Try: <?php if(get_post_meta($post->ID, 'fieldname', true)){ echo "<p><strong>Label:</strong> ". get_post_meta($post->ID, 'fieldname', true); ."</p>\n"; } ?>
March 16, 201214 yr Try: <?php if(get_post_meta($post->ID, 'fieldname', true)){ echo "<p><strong>Label:</strong> ". get_post_meta($post->ID, 'fieldname', true); ."</p>\n"; } ?> There's no need to call the function twice (it's just a waste!) - do something like: <?php if(($meta = get_post_meta($post->ID, 'fieldname', true)) != ""){ echo "<p><strong>Label:</strong> ". $meta ."</p>\n"; } ?>
March 16, 201214 yr There's no need to call the function twice (it's just a waste!) - do something like: <?php if(($meta = get_post_meta($post->ID, 'fieldname', true)) != ""){ echo "<p><strong>Label:</strong> ". $meta ."</p>\n"; } ?> thats the same thing lol just in a variable
March 16, 201214 yr thats the same thing lol just in a variable it's quite different because he only calls the function once and assigns the value to the variable. but yeah, they work either way and I doubt there'd be any significant time difference.
Create an account or sign in to comment