Skip Navigation

[Closed] Conditional Block

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 12 replies, has 2 voices.

Last updated by jww 1 year, 4 months ago.

Assisted by: Nigel.

Author
Posts
#2638147

jww

I have a conditional block.
I have a fields and text block inside the conditional block.

I have two scenarios:
1. I want to have two conditions. If the field is empty, don't display the block. I have that set and it works fine.
But I also want the condition that if the field is not empty but contains a 0, to also not show it.
In the Toolset documentation here
https://toolset.com/course-lesson/using-toolset-conditional-block/
It says that a plus sign should appear to add another condition. I don't see any plus sign or any way to add another condition, nor, and/or as it says in the doc.

2. In another conditional block I want to show data from field B if data from field A is empty.
So...show field A if it has something in it but if it's empty, then show the data from field B.
I don't see how to do this.

Thanks.

#2638265

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

We can ignore what's inside the conditional block when discussing them as you can put anything inside them, and what is conditionally displayed has no effect on the condition being evaluated that determines whether they display or don't.

I just checked trying to add multiple conditions with a conditional block, and you are right—the + sign to add extra conditions is missing.

I'm going to escalate that to the developers to be addressed.

But in the meantime to able to add multiple conditions it will be necessary to use the advanced editor and add the conditions manually.

I think in the first case you describe you would need to add a condition something like this (you'd need to adjust the field slug):

( ( empty($(wpcf-my-custom-field)) )  OR ( $(wpcf-my-custom-field) eq '0' ) )

(It may help to check the legacy documentation for adding conditions via shortcodes to get an idea of the syntax involved: https://toolset.com/documentation/legacy-features/views-plugin/conditional-html-output-in-views/)

For your second example you effectively want an if...then...else structure (if A is not empty show A else show B) which isn't directly possible.

You need to add two Conditional blocks, with opposite conditions.

So first block is "if A is empty" and the content will be B.

Second block is "if A is not empty" and the content will be A.

#2638321

jww

Thanks, Nigel.
The first one:
Worked perfectly, thank you. Question about the logic. I have
NOT ( ( empty($(wpcf-my-custom-field)) )
which works. Is that meaning: "Show this block if that custom field is NOT empty"?

In the second example, the two different conditional blocks would just be normal style one after another, not nested, correct?
Also, if I select "Empty Value in the drop down, then the choices on the right disappear to select. So I don't see a way to say if A is empty then show B. I see all the greater than, less than, != etc but if you choose empty value then all right side options go away.

#2638325

jww

Actually in scenario one, it didn't work. I looks like it does but after you save the page it changes it. Strange. Also what I want is it to show only if it is empty or NOT equal to 0.
So where does the not go in the second instance
( NOT ( empty( $(my-custom-field)) ) OR ( $(wpcf-my-custom-field) eq '0' ) )
Like this?
( NOT ( empty( $(my-custom-field)) ) OR NOT( $(wpcf-my-custom-field) eq '0' ) )

Also why in the first brackets is it my-custom-field and in the second wpcf-my-custom-field?

#2638329

jww

Strange, if I add the OR condition in the advanced editor, then the dialogue with and/or shows up (like as if the plus button were there to add it. So, that lets me see how to add the NOT in the second instance.

#2638333

jww

I don't mean the second scenario, I mean the second bit in the first scenario after the OR.

#2638347

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

>Is that meaning: "Show this block if that custom field is NOT empty"?

Yes. You can test if a field is empty, and you can also test the opposite (not empty).

>In the second example, the two different conditional blocks would just be normal style one after another, not nested, correct?

That's correct.

One block to show something (field B) if A is empty.
One block to show something (field A) if A is not empty.

>Also, if I select "Empty Value in the drop down, then the choices on the right disappear to select. So I don't see a way to say if A is empty then show B.

That doesn't make sense, inasmuch as what you display (field B) has nothing to do with the condition, rather it is what you add inside the Conditional block to show when the condition is met.

I'm getting a bit lost after that.

When Types custom fields are saved to the wp_postmeta table a 'wpcf-' prefix is added to the key, so a Type custom field with a slug of 'my-custom-field' would be saved with a metakey of 'wpcf-my-custom-field', which is what I'm using in the examples above.

If you want the test to be to check the field is != 0 then that is written as

$(wpcf-my-custom-field) ne '0'
#2639493

jww

I have the conditional block on a search page.
The view is a custom field checkbox.
I only want the block to show if any of the boxes are checked.
But when I go to the settings for the block, since it's a page (where the Search filters and map are set up) it doesn't know I'm talking about that value for that post. It's only giving me the options for the page, not the custom post type > custom field.
How would I set up the conditional block to only show what's in it IF what's in it has a value where it isn't giving me any options for that custom post type?

#2639555

jww

Also in the conditional block logic...I don't get why this is happening:
I have a custom post type.
I have a template for that custom post type.
I have posts in that custom post type.
On the template, I have a conditional block, which we discussed above with the two nots to say don't show it if the value is empty or is 0. That is working well for most fields using that logic.
However on one field, with the exact same logic as the others, it works on the front end for some posts and not for others. In other words posts using the same template with that conditional block are having different results on the front end which makes no sense to me. They are using the same template. The text of the fields and texts block and the 0 show up on one post but not on another. How is that possible? They are all the same post type using the same Toolset template.

#2639569

jww
Screenshot 2023-08-30 at 4.45.01 PM.png

And now something really weird happened. I have about 20 conditional blocks on my CPT template page.
I had all of the conditions set with the double not as discussed above. Most were working with some oddities.
But now every block says "incomplete condition" and all of my 20 blocks are goofy. 20 minutes ago they all spelled out the conditions. Now they are all broken? I hadn't made any changes to anything. Why would this happen??

#2639581

jww

So now I've re-set all of them. They all became unset when I added the second condition.

#2639793

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2023-08-31 at 15.41.09.png

We have a known issue about this.

It's not very helpful, where in the sidebar it reports the manually entered conditions as incomplete, but they should still work as intended. If you open them to edit them again, you should see that the conditions are still there, as entered (screenshot).

If that's what you experience then we know about it and it is on our to-do list. If you find something else, please let me know.

#2640009

jww

When I opened them the conditions were sometimes there and sometimes not and had an extra not. They were all wonky and I had to rebuild them all.

The topic ‘[Closed] Conditional Block’ is closed to new replies.