19th Jun '24
/
1 comment

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 query loop's parent initially had these classes:

grid--4 grid--l-3 grid--m-1 list--none align-items--center

These are ACSS classes that lays out the list items in a 4-column (on desktops) grid.

When there are more than 4 team members in any row, they will wrap down to a new line in the CSS grid.

The requirement is to show the items in 5 columns if the number of team members in a department is 5 and in 6 columns if the number of team members in a department is 6.

So, based on the query result count of a Bricks query loop, the class grid--4 should either be grid--4 or grid--5 or grid--6.

If the query result count is 5, the class should be grid--5.

If the query result count is 6, the class should be grid--6.

For all other query result counts, grid--4.

This Pro tutorial provides the steps to accomplish the above based on the How to programmatically add a class to an element in Bricks tutorial.

Step 1

Add the following in child theme's functions.php (w/o the opening PHP tag) or a code snippets plugin:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 630 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Conditional Rendering for Logged in/out Users by CSS Classes in Bricks

Conditional Rendering for Logged in/out Users by CSS Classes in Bricks

This tutorial shows how Bricks builder's bricks/element/render filter can be used to conditionally output elements having a class of logged-in to logged-in users only and…
Categories:
Check if a given category has at least one post

Check if a given category has at least one post

There could be situations where you want to check if a given category (by its slug) has at least 1 post assigned to that category.…
Categories:
Pro
Conditional Section when Post has a Featured Image in Bricks

Conditional Section when Post has a Featured Image in Bricks

This Pro tutorial provides the steps to output a Section in Bricks on single posts (of any post type) only if the post has a…
Categories:
Pro
At Least 1 Search Result Condition in Bricks

At Least 1 Search Result Condition in Bricks

Registering a custom condition to render elements depending on whether there is at least one search result.
Categories:
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.
Pro
Month and Day Condition in Bricks

Month and Day Condition in Bricks

This Pro tutorial shows how we can define a custom function that takes in a month in the three-letter format (like Dec) and a day…
Categories:
JetEngine Checkbox Condition in Bricks

JetEngine Checkbox Condition in Bricks

How elements in Bricks can be conditionally output depending on the selected option from a JetEngine checkbox field.
Categories:
Tags: