Skip Navigation

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

This support ticket is created hace 3 años, 10 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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)

Etiquetado: 

Este tema contiene 1 respuesta, tiene 2 mensajes.

Última actualización por Jamal hace 3 años, 10 meses.

Asistido por: Jamal.

Autor
Mensajes
#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

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.