Skip Navigation

[Resolved] Not showing a Custom field if it’s empty

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

Problem:

I have a view inside my product template that show custom repeatable field groups. I would like to hide the full section if there's no items in that custom field groups.

Solution:

Toolset repeatable field group is based on one-to-many relationship, each item of "Price variation" is an actual post.

I suggest you try to move those outer HTML tags into the post view, within shortcode [wpv-items-found] ... [/wpv-items-found] , for example:

https://toolset.com/forums/topic/not-showing-a-custom-field-if-its-empty/#post-1208064

Relevant Documentation:

This support ticket is created 5 years, 1 month 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by jean-francoisB 5 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1205159
bad-prez.jpg
good-prez.jpg

Tell us what you are trying to do?

I have a view inside my product template that show custom fields. I would like to hide the full section if there's no information in that custom field.

This the code I'm trying to use now that doesn't work :

[wpv-conditional if="( NOT(empty($(product-variation))) )"]
<div class="row">
<hr>
</div>

<div class="row">
<h4>Options</h4>
</div>

<div id="prod-options" class="row">
[wpv-view name="product-variation" orderby="ID" order="asc"]
</div>

[/wpv-conditional]

So if the custom fields product-variation is not empty, show the section. Else, hide the section.

I tried to use the conditional-output function but can't see my custom fields in the dropdown menu.

Is there any documentation that you are following?

https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-fields-and-other-elements-for-emptynon-empty-values/

Is there a similar example that we can see?

What is the link to your site?

Example product page showing the section I would like to hide if empty : hidden link

See images attached of what I want when is NOT empty and what section I want to hide if empty.

Thank you for your help!

#1205333

Hello,

I assume the custom field "prod-options" is created with Toolset Types plugin.

If it is, Types plugin will pre-pend "wpcf-" slug before the field slug, please try to modify this line of your code from:

[wpv-conditional if="( NOT(empty($(product-variation))) )"]

To:

[wpv-conditional if="( NOT(empty($(wpcf-product-variation))) )"]

And test again.

#1206011
Screenshot_2019-02-26 Edit Group ‹ John's Bedroom — WordPress.png

Hi,

Thanks for your help, but this didn't work.

This is my code that I use :

[wpv-conditional if="( NOT(empty($(wpcf-product-variations))) )"]
<div class="row">
	<hr>
</div>
   

<div class="row">
 <h4>Options</h4>
  </div>
    
    
<div id="prod-options" class="row">
	[wpv-view name="product-variation" orderby="ID" order="asc"]
</div>
[/wpv-conditional]

You can see the detail about my custom field in attached file. At the moment that there's no information filled in that field, I want the full section to be hided. At the moment there's something, the section is showed.

Hope we can find a solution.

Thank you

#1206036

I have tried the same codes in my localhost with a fresh wordpress installation + the latest version of Toolset plugins.

It works fine, please check these:
1) You are using the latest version of Toolset plugins, you can download them here:
https://toolset.com/account/downloads/

2) In case it is a compatibility problem, please deactivate other plugins, and switch to wordpress default theme 2019, and test again

3) If the problem still persists, please provide database dump file(ZIP file) of your website, also point out the problem page URL, I need to test and debug it in my localhost, thanks
https://toolset.com/faq/provide-supporters-copy-site/

#1208064

Thanks for the details, you are using a repeatable field group "Price variation", Toolset repeatable field group is based on one-to-many relationship, each item of "Price variation" is an actual post, so wpv-conditional shortcode won't work as expected.

I suggest you try to move those outer HTML tags into the post view, within shortcode [wpv-items-found] ... [/wpv-items-found] , for example:
1) Edit the content template "Template for Products"
hidden link
remove the outer HTML div tags, move them into below post view:

2) Edit the post view "Product variation"
hidden link

in section "Loop Editor", paste those HTML div tags insider shortcode [wpv-items-found] ... [/wpv-items-found].

And if you don't want to display anything when there isn't result found, you can remove these codes:

<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>

More help:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-items-found
Meta HTML wrapped in [wpv-items-found][/wpv-items-found] will be output if there are any posts | taxonomies | users returned by the View.

#1208074

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.