I am trying to get Toolset to ignore blank fields when outputting Custom Fields. Please see screenshots attached.
I am using the 'Current Code' to get the following results.
When all fields have a value, the output shows a complete list. This is as expected.
When one of the fields doesn't have a value, the output shows the list but with a gap where the no value field is.
What I want to achieve is what is shown in the 'Desired output' screenshot. I would like it to behave that so when there is a field with no value, this field is effectively ignored, or at least hidden so there is no gap between other populated fields.
What is the best way to go about doing this? I have had a look through the Toolset documentation including articles about Conditional Outputs but I have no idea how to achieve this myself.
To do this, you need to show/hide things conditionally.
Right now as you say, if all fields have values all is fine, since your HTML (break tags and ShortCodes) will just show fine on the front end.
As soon one of those Fields has no value nothing can be shown for it, but the HTML still is used.
In this cases, you end up with empty "spaces" or gaps, which you would like to then hide.
That can be done with Conditional Toolset HTML:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
An example:
[wpv-conditional if="( NOT(empty($(wpcf-free-link-one))) )"]
[types field="free-link-1" title="Free Link One" target="_blank"][/types]
[/wpv-conditional]
That would show only if the Field has a value.
So you can as well wrap HTML in those conditions, or any other content.
[wpv-conditional if="( NOT(empty($(wpcf-free-link-one))) )"]
[types field="free-link-1" title="Free Link One" target="_blank"][/types]
<br>
[/wpv-conditional]
[wpv-conditional if="( NOT(empty($(wpcf-free-link-two))) )"]
[types field="free-link-2" title="Free Link Two" target="_blank"][/types]
<br>
[/wpv-conditional]
etc
Thank you Beda.
That works brilliantly!
One further question, is it possible to insert an "if not empty' rule using the Insert conditional shortcode GUI?
I think I may have found it using the options in the attached screenshot. But that gives a slightly different output than you suggested above.
[wpv-conditional if="( $(wpcf-free-link-1) ne '' )"][/wpv-conditional]
Are there any differences between this way of doing it and the code you suggested above? Is either a better way of doing it or are they exactly the same?
They're basically the same, and either version should work equally well.