Skip Navigation

[Gelöst] Conditional Block Based on View >0 Items Found

This support ticket is created vor 3 Jahre, 3 Monate. 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: Africa/Casablanca (GMT+01:00)

This topic contains 1 Antwort, has 2 Stimmen.

Last updated by Jamal vor 3 Jahre, 3 Monate.

Assisted by: Jamal.

Author
Artikel
#1892139

I currently have a content template that displays a view on the right-hand column. I'd like to put a border around this view but only if the view returns any rows. Is it possible to put this logic in the content template itself (i.e. do not put the logic in the view itself, but the block that contains the view)? The reason I'd like to put the conditional styling in the content template instead of the view is so I can use the same view in other areas of the website without putting a border around it. Thanks in advance, and happy New Year.

- Aaron

#1892463

Jamal
Supporter

Languages: Englisch (English ) Französisch (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Aaron and thank you for contacting the Toolset support.

I can see two ways to achieve what you are looking for. One way that will be part of the view, using Javascript. And another way will include some custom PHP code and conditional block in the content template.

The first way would be to add some custom Javascript code that will check if the view has any results and apply the border only if the view is used inside that content template. For example, you can add a CSS ID or class to the content template. The Javascript code will check if the view has results and inside that content template, then it will apply the border.

The other way would be to create a custom function or shortcode that will check if the view has results and use it in a conditional block. This custom code can use the get_view_query_results function to check if the view has results.
https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results

Then you will need to register the function or the shortcode in Toolset Settings in order to be able to use it inside a conditional block. Check these articles:
- https://toolset.com/course-lesson/using-toolset-conditional-block/#checking-for-shortcodes-values
- https://toolset.com/documentation/programmer-reference/views/using-custom-functions-in-conditions/
- https://toolset.com/documentation/programmer-reference/displaying-custom-functions-with-views-and-using-them-in-conditional-logic/

I hope this helps. Let me know if you have any questions.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.