Skip Navigation

[Résolu] Cannot get conditional shortcode to work

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I have two custom fields that contain numeric values. I would like to use conditional shortcodes to display the first field value if it is not empty or '0'. If the first field value is empty or '0', I would like to display the second field value.

Solution: Use the following two conditional shortcodes to achieve this effect:

<tr>
      <td><span class="invoice-info-label">FOB Fee:</span></td>
      <td><span class="invoice-info-amount"> 
        [wpv-conditional if="( ('[types field="fob-fee" output="raw"][/types]' ne '0') AND ('[types field="fob-fee" output="raw"][/types]' ne '') )"]
        [format-no-decimals num="[types field='fob-fee' format='FIELD_VALUE'][/types]"][/format-no-decimals]
        [/wpv-conditional]
        [wpv-conditional if="( ('[types field="fob-fee" output="raw"][/types]' eq '0' ) OR ('[types field="fob-fee" output="raw"][/types]' eq '' ))" ]
        [format-no-decimals num="[types field='suggested-fob-fee' format='FIELD_VALUE'][/types]"][/format-no-decimals]
        [/wpv-conditional]
        </span></td>
    </tr>

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-conditional
https://toolset.com/course-lesson/using-toolset-conditional-block/

This support ticket is created Il y a 3 années et 9 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Marqué : 

Ce sujet contient 9 réponses, a 4 voix.

Dernière mise à jour par PaulS4783 Il y a 3 années et 9 mois.

Assisté par: Christian Cox.

Auteur
Publications
#1943197
[wpv-conditional if="  ( ( '1' eq '2' ) ) " ]
      <td><span class="invoice-info-label">FOB Fee:</span></td>
      <td><span class="invoice-info-amount"> [format-no-decimals num='[types field='suggested-fob-fee' format='FIELD_VALUE'][/types]']</span></td>
[/wpv-conditional] 

So in the above code I would expect NOTHING to be rendered since "1" obviously does not equal "2".
And yet that row output is rendered as if the conditional statement was completely ignored.

What I would ultimately like to do is have a conditional statement that checks the value of a (numeric) field.
If the value is zero or blank, then use a "suggested" value.

So something like this:

[wpv-conditional if="  ( ( '[types field='fob-fee' format='FIELD_VALUE'][/types]' eq '0' ) ) " ]
      <td><span class="invoice-info-label">FOB Fee:</span></td>
      <td><span class="invoice-info-amount"> [format-no-decimals num='[types field='suggested-fob-fee' format='FIELD_VALUE'][/types]']</span></td>
[/wpv-conditional] 

Any ideas about what is going wrong?

#1943259

Bueller?

#1944185

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Paul,

Thank you for getting in touch. I must apologize for the delay in response.

I see from your shortcode that you've added an extra open and close brackets. Can you format it like below and let me know if it produces a result.

[wpv-conditional if="  (  '[types field='fob-fee' format='FIELD_VALUE'][/types]' eq '0'  ) " ]
      <td><span class="invoice-info-label">FOB Fee:</span></td>
      <td><span class="invoice-info-amount"> [format-no-decimals num='[types field='suggested-fob-fee' format='FIELD_VALUE'][/types]']</span></td>
[/wpv-conditional] 

If no results are produced then you will need to activate the debug mode for the conditional and copy and send me the debug log that will be printed out on the frontend.

To enable the debug for the conditional please add debug='true' as an attribute for the shortcode, example

[wpv-conditional if="  (  '[types field='fob-fee' format='FIELD_VALUE'][/types]' eq '0'  ) " debug="true" ]

Looking forward to hearing from you soon.

Thanks,
Shane

#1948205

So here is the updated conditional short code:

[wpv-conditional if="  (  '1' eq '0'  ) " debug="true" ]
      <tr><td><span class="invoice-info-label">New FOB Fee:</span></td>
      <td><span class="invoice-info-amount"> [format-no-decimals num='[types field='suggested-fob-fee' format='FIELD_VALUE'][/types]']</span></td></tr>
[/wpv-conditional]

And since '1' doesn't equal '0' I would expect this row to not get rendered.

However it does.
lien caché

####################
wpv-conditional attributes
####################
Array
(
    [if] =>   (  '1' = '0'  ) 
    [debug] => true
)

####################
Debug information
####################
--------------------
Original expression:   (  '1' = '0'  ) 
--------------------
After replacing 1 general variables and comparing strings: (  1 = 0  )
	Comparing 1 to 0
#1951929

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Screenshot 2021-02-16 at 14.47.56.png

Hi there

Shane is on vacation now and I wanted to follow up here.

I've tested this myself on my own site and it works as expected.

Here's the conditional shortcode I added to a page, and you can see the results in the screenshot:

[wpv-conditional if="( '1' eq '0' )" debug="true"]
<p>Some hidden content</p>
[/wpv-conditional]

The interior content is not displayed, because the condition fails.

Are you sure that the content that appears on the front-end comes from within the conditional block and isn't output by something else?

If you edit your conditional shortcode and just include some placeholder text inside any other markup or shortcodes, does it still get shown incorrectly?

What is the context where you are using this?

I was just testing this out on a static page. You are using it in a View or elsewhere?

#1952939

OK. I have figured out what appears to be a ToolSet bug.

When I use this:

[wpv-conditional if="( '1' eq '0' )" debug="false"]
      <p>Paragraph content</p>
[/wpv-conditional]

the output is NOT rendered.

BUT when I use this:

[wpv-conditional if="( '1' eq '0' )" debug="false"]
      <td>Table data cell content</td>
[/wpv-conditional]

the output IS rendered.

So apparently, conditional short codes will not work on <td> tag content.

I am using the Views Module, not the Blocks Module.
Should that make a difference?

I am trying to create a "Vehicles" post type template.
I find the new Blocks module difficult to work with so I would prefer to use short codes within the Gutenberg editor.

#1953907
Screen Shot 2021-02-17 at 7.34.20 AM.png
Screen Shot 2021-02-17 at 7.34.02 AM.png

BUT when I use this:

[wpv-conditional if="( '1' eq '0' )" debug="false"]
      <td>Table data cell content</td>
[/wpv-conditional]

the output IS rendered.
So apparently, conditional short codes will not work on <td> tag content.

I'm not able to reproduce this in a simple test, perhaps something else is going on I'm not able to see? I'd need to see the full context here to be able to determine what is happening. Sometimes the browser will render an empty table cell if necessary to preserve the table structure in a table with multiple rows, and I suspect that is what you're seeing. I'm attaching some screenshots here showing a table cell properly hidden by the same conditional you shared (cell 2 is included in the conditional and suppressed on the front-end).

I'm activating private reply fields here so you can share a login if you'd like for me to investigate further on your site, or I can set up a sandbox site where we can collaborate.

#1956407

Okay I was able to get this conditional and table structure working as intended with some modifications. Here is the original code for the FOB table row:

    <tr>
      <td><span class="invoice-info-label">FOB Fee:</span></td>
      <td><span class="invoice-info-amount"> [format-no-decimals num='[types field='suggested-fob-fee' format='FIELD_VALUE'][/types]']</span></td>
    </tr>

Here is the update with conditionals in place:

    <tr>
      <td><span class="invoice-info-label">FOB Fee:</span></td>
      <td><span class="invoice-info-amount"> 
        [wpv-conditional if="( '[types field="fob-fee" output="raw"][/types]' ne '0' )"]
        	[types field='fob-fee' format='FIELD_VALUE'][/types]
        [/wpv-conditional]
        [wpv-conditional if="( '[types field="fob-fee" output="raw"][/types]' eq '0' )" ]
        	[types field="suggested-fob-fee" format="FIELD_VALUE"][/types]
        [/wpv-conditional]
       </span></td>
    </tr>

Now the fob-fee field displays if it's not '0', and the suggested-fob-fee field displays if fob-fee is '0'. Hint - always use output="raw" with Types fields in a conditional, to prevent formatting conflicts.

One thing I'm still working on is formatting those fields using the format-no-decimals shortcode. Whenever I add that inside the conditionals, I see rendering problems. I'm trying to resolve that now and I'll give you an update shortly.

#1956469

Okay I found the issue with the formatting shortcode and it was a pretty simple fix. The problem is that wpv-conditional is temperamental with shortcode syntax, and if you don't include closing tags for custom shortcodes it can have trouble parsing the contents of the conditional. I noticed that none of the format-no-decimals shortcodes in this template had closing tags, so I edited each instance of the shortcode and added an explicit closing tag. Now I'm able to use the format-no-decimals shortcodes successfully in the contents of these conditionals, and everything seems to be displayed correctly.

I also realized that I had overlooked the requirement to show the suggested FOB fee if the actual FOB fee value is empty ( as well as if the actual FOB fee value is 0). I've updated the conditionals here to handle both cases. You can see the implementation here with the format-no-decimals shortcodes in place:

<tr>
      <td><span class="invoice-info-label">FOB Fee:</span></td>
      <td><span class="invoice-info-amount"> 
        [wpv-conditional if="( ('[types field="fob-fee" output="raw"][/types]' ne '0') AND ('[types field="fob-fee" output="raw"][/types]' ne '') )"]
        [format-no-decimals num="[types field='fob-fee' format='FIELD_VALUE'][/types]"][/format-no-decimals]
        [/wpv-conditional]
        [wpv-conditional if="( ('[types field="fob-fee" output="raw"][/types]' eq '0' ) OR ('[types field="fob-fee" output="raw"][/types]' eq '' ))" ]
        [format-no-decimals num="[types field='suggested-fob-fee' format='FIELD_VALUE'][/types]"][/format-no-decimals]
        [/wpv-conditional]
        </span></td>
    </tr>

So in summary:
- Always include an explicit closing tag for custom shortcodes, especially inside wpv-conditional contents
- Always use output="raw" with Types fields in wpv-conditional conditional clauses and avoid using the Types field shortcode "format" attribute
- A single wpv-conditional clause should only use one pair of parentheses UNLESS you're combining multiple independent clauses using "AND" or "OR". Then, each clause must be enclosed in a pair parentheses, and another pair of parenthesis should wrap around all the independent clauses:

Good!

[wpv-conditional if="( '1' eq '0' )"]

Good!

[wpv-conditional if="( ( '1' eq '0' ) AND ( '1' eq '2' ) )"]

Bad!

[wpv-conditional if="( ( '1' eq '0' ) )"]

Bad!

[wpv-conditional if=" ( '1' eq '0' ) AND ( '1' eq '2' ) "]
#1957413

Thanks Christian!
You are a star.

You can close this ticket.