Conditional Rendering in Bricks Based on CSS Class

Updated on 19 Jun 2024

A user asks:

Is there a way to apply the same conditions to several elements like we set styles appending a CSS class?

I have a button with class .btn-accent with dynamic condition to check if a function is equal to 1.
I'd like to set this same visibility conditions to other elements with same class.

Bricks has a great filter hook called bricks/element/render that can be used to programmatically control the rendering (output) of elements.

CSS class-based rendering is provided in example #2 on the above page.

This Pro tutorial provides another example of using the bricks/element/render filter to output any element having a class of render-if-status-is-checked only if a "Status" True / False type of custom field is checked on an ACF Options page.

After implementing the tutorial, to output an element based on the Options page field value all you have to do is assign the CSS class to the element. You are going to tell Bricks to check the list of classes for the elements before it renders the elements and if the CSS class name you specify is present for an element then it is going to run a function that returns a true or false. If it receives true, then the element is rendered. Otherwise, it won't.

This is easier and quicker than using the Conditions dialog if you have a number of elements on which the condition should be applied.

Step 1

Install and activate ACF Pro.

Go to ACF → Options Pages.

Add a new Options Page.

Step 2

Create a field group having a True / False field attached to the above Options page.

In the Presentation tab for the field, you may want to toggle "Stylized UI" on.

Step 3

Click on Site Settings menu in the left admin menu.

Turn on the Status (technically speaking, it is a checkbox - so you are actually ticking it).

Click Update.

Step 4

Back in child theme's functions.php or your code snippets plugin, add:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 526 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Conditional single post Bricks template based on internal referrer URL

Conditional single post Bricks template based on internal referrer URL

How to automatically select a single post template depending on the page from which the post is accessed.
[Function] Current Term Has Children

[Function] Current Term Has Children

Updated on 20 Jul 2024 A user asks: I'm working on an archive template for WooCommerce products. I'm trying to hide a section if there…
Conditional Output on the First Page of Archives in Bricks

Conditional Output on the First Page of Archives in Bricks

How to output an element on an archive page only if we're on the first page of the archive.
Categories:
Pro
Custom Conditions in Bricks Builder

Custom Conditions in Bricks Builder

Updated on 22 Jun 2022 This Pro tutorial is a continuation of the previous Conditions in Bricks Builder tutorial. We are going to keep updating…
Categories:
Pro
Conditional CSS Classes based on Bricks Query Count

Conditional CSS Classes based on Bricks Query Count

In a Bricks project I am currently working on, there are nested query loops - team members inside departments on a Page. Each team member…
Conditions in Bricks Builder

Conditions in Bricks Builder

The current version of Bricks does not have a built-in Conditions feature but that does not mean we can't set them up manually with custom…
Categories:
Pro
Conditional Rendering of ACF Repeater Rows Based on True / False Sub Field in Bricks

Conditional Rendering of ACF Repeater Rows Based on True / False Sub Field in Bricks

This Pro tutorial provides the steps to modify a ACF Repeater query loop to only output the rows for which a True / False type…
Categories: