[Resolved] Cannot get conditional shortcode to work
This thread is resolved. Here is a description of the problem and solution.
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:
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.
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
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?
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.
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.
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:
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.
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:
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: