Adding active class to the current term/post in a Bricks query loop on Term archives/single posts

Updated on 29 Feb 2024

In the Sibling Terms Bricks Query Loop tutorial, a member asked:

How can I set the class “active” to the current category ?

This Pro tutorial shows how.

This also applies to a posts query loop when viewing a single post, not just terms.

Step 1

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

<?php 

function bl_maybe_set_active_class(): string {
    return BricksQuery::get_loop_object_id() === get_queried_object_id() ? 'active' : '';
}

Step 2

Edit the element on which you’d like to apply the active class. This could be the query-loop enabled element itself or a child element inside.

Go to STYLE → ATTRIBUTES and add a class attribute like this:

{echo:bl_maybe_set_active_class}