Skip Navigation

[Resolved] Conditional Block Based on View >0 Items Found

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.

This topic contains 1 reply, has 2 voices.

Last updated by Jamal 8 months, 3 weeks ago.

Assigned support staff: Jamal.

Author
Posts
#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: English (English ) French (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.