Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Display Text Only If Field Is Populated

Featured Replies

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

Try:

 

<?php
if(get_post_meta($post->ID, 'fieldname', true)){
  echo "<p><strong>Label:</strong> ". get_post_meta($post->ID, 'fieldname', true); ."</p>\n";
}
?>

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";
}
?>

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

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.