Skip Navigation

[Resolved] How to hide empty fields from the output and instead show something else?

This thread is resolved. Here is a description of the problem and solution.

Problem:
Sometimes Custom Fields or other data is empty, and the shortcodes displaying it will of course not output any data.
However wrapping HTML, or labels or any other content we add around those ShortCodes will still be output when the field is empty.
How can we avoid that?

Solution:
You can use HTML conditionals.
It's explained in more depth here, also a good reference and cheatsheet:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
You can insert those conditions with the "Conditional Output" or "if" button, here is a short example showing a field's content if it's saved, or "nothing", if there is nothing saved for that field:

[wpv-conditional if="( $(wpcf-work-experience) ne '' )"]Label: [types field="my-field" output='raw'][/types][/wpv-conditional][wpv-conditional if="( $(wpcf-my-field) eq '' )"]NOTHING[/wpv-conditional]
This support ticket is created 4 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by deepS 4 years, 9 months ago.

Assisted by: Beda.

Author
Posts
#1280065
Screenshot_1.jpg

Tell us what you are trying to do?

I am making a job site. I have created a view with a repeatable group (custom) field for job details. There is a field called 'work experience'. But it's often seen that for some jobs no experience is required, hence the custom field remains empty in those cases. The code is created in this way:

<h3>Name of the Post: [types field="post-name"][/types]</h3>
<ul>
<li>No. of Vacancies: [types field="vacancies"][/types]</li>
<li>Uppre Age Limit: [types field="upper-age-limit"][/types]</li>
<li>Educational Qualification: [types field="educational-qualification" output='raw'][/types]</li>
<li>Work Experience: [types field="work-experience" output='raw'][/types]</li>
<li>Pay Scale: [types field="pay-scale"][/types]</li>
</ul>

Now even if the work experience field is empty, the title of the field doesn't obliterate. Hence it looks very odd like in the screenshot 1.
Please help me to get rid of this

#1280083

You'd need to use HTML Conditions here.
As example wrap your list items in a condition that checks whether an entry exists or not, and accordingly displays content or not:

[wpv-conditional if="( $(wpcf-work-experience) ne '' )"]
<li>Work Experience: [types field="work-experience" output='raw'][/types]</li>
[/wpv-conditional]

It's explained in more depth here, also a good reference and cheatsheet:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

The GUI to build the conditions can be found with these steps:
1. Highlight the part you want to show conditionally with your mouse
2. In the Toolbar of the text editor find the "Conditional Output" or "If" Button and click it
3. Construct the condition according to the instruction of the GUI
4. Insert it.

#1280151

Thanks for your support. I would appreciate if you help me a little bit like displaying a text in the same space like 'Freshers may apply', rather than hiding the title. Thanks again.

#1281651

In this case, you would still use a HTML condition, as my example shows, just, you would output 'Freshers may apply' instead of the ShortCode, when the field is empty.
When not, then you would output the shortcode:

<li>[wpv-conditional if="( $(wpcf-work-experience) ne '' )"]Work Experience: [types field="work-experience" output='raw'][/types][/wpv-conditional][wpv-conditional if="( $(wpcf-work-experience) eq '' )"]Freshers may apply[/wpv-conditional]</li>

The best would be if you use the GUI for the conditional because there you can add all logic and condition with a User Interface, without need to edit the code.

#1282725

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.