Skip Navigation

[Résolu] HTML conditional not working with Custom ShortCode

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

Problem:
Whenever I ues Custom ShortCodes in HTML conditionals, then the conditions fail, and do not even output debug information anymore if `debug="true"` is set.

Solution:
To use Custom ShortCodes (or ShortCodes of other Themes/Plugins) in HTML Conditionals, you need to register those ShortCodes first in Toolset > Settings > Front end content > Third-party shortcode arguments.

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/#checking-custom-shortcodes

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

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

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 2 réponses, has 2 voix.

Last updated by martinE-4 Il y a 4 années et 10 mois.

Assisted by: Beda.

Auteur
Publications
#1269653

I have a conditional in a shortcode block in my view:

[wpv-conditional if="( '[count_instances field= 'wpcf-field_resource_link_title' ]'  >  '1' )" debug="true"]<p style="margin: 0; padding: 0"><b>Resources:</b></p>[/wpv-conditional]

Its not working (no debug output even with debug switch true).

I expected my shortcode to give a value > 1 (I tested it on a post for which it has the value 2) and the conditional if to be true.

But instead nothing happened.

Do I have a syntax error? I checked here:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

#1269755

It seems count_instances is not a ShortCode of Toolset.
Given this is a 3rd party (custom code or other software) you would have to register it for usage in conditionals under Toolset > Settings > Front end content > Third-party shortcode arguments.

If that doesn't already solve the issue, it depends on what your ShortCode is returning, because classically an HTML conditional will work if you compare "proper" data to related data, you can check this by comparing if "1 eq 1" in the Conditional for example.

If that works in your case, then the issue is due to the values given back by the ShortCode (Custom Method) or whatever value used in the comparison operators.

1. May I ask if you used this ShortCode Method here?
https://toolset.com/forums/topic/count-number-of-entries-in-repeating-field/#post-1207355
==> There should be easier methods as well to do that:
https://toolset.com/forums/topic/how-to-count-repeated-field/#post-412049
2. In either case, the main questions are:
- what does count_instances output on its own if inserted to the Loop?
- does it produce any HTML around its value?
- is the value correct?

If it produces any HTML the condition will break, if it produces a slightly different value, it won't work either.

If all this is not the case - the only remaining issue could be the apostrophes not interchanging (" should alternate with ', hence [wpv-conditional if="( '[count_instances field= "wpcf-field_resource_link_title"]' > '1' )" debug="true"]<p style="margin: 0; padding: 0"><b>Resources:</b></p>[/wpv-conditional])

Please let me know if any of the above helped to solve this issue.

#1269899

The problem was not registering it as a 3rd party shortcode. 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.