10th Sep '24
/
0 comments

Term Parent Slug Data Attribute in Bricks

Term Parent Slug Data Attribute in Bricks

In the BricksLabs Facebook group, a user asks:

How can I set a data attribute on a child taxonomy based on the parent tax?

In this case, I want to change the background color of the {tax_name:link} element based on the parent taxonomy.

This Pro tutorial shows how we can define a custom function that takes a term ID as a parameter (defaulting to the current term in the loop) and returns the slug of the parent term to be used as the value of a data attribute in a Bricks Terms query loop.

After implementing the tutorial, given these categories,

HTML output on the front end:

It is then possible to uniquely color code your terms or add an icon based on its parent term.

Ex.:

[data-parent-term="food"] a { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='24px' viewBox='0 0 24 24' width='24px' fill='%235f6368'%3E%3Cpath d='M0 0h24v24H0z' fill='none'/%3E%3Cpath d='M11 9H9V2H7v7H5V2H3v7c0 2.12 1.66 3.84 3.75 3.97V22h2.5v-9.03C11.34 12.84 13 11.12 13 9V2h-2v7zm5-3v8h2.5v8H21V2c-2.76 0-5 2.24-5 4z'/%3E%3C/svg%3E"); background-repeat: no-repeat; padding-left: 1.6em; }

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 610 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Bricks Query Loop for Top-level Parent Pages with Children

Bricks Query Loop for Top-level Parent Pages with Children

In the Bricks Facebook group, a user asked: Looking for a custom query to output a list of posts in the same hierarchy that the…
Categories:
Loop Item ID and Loop Item Object in Bricks

Loop Item ID and Loop Item Object in Bricks

If you are working on a Bricks site with specific requirements, chances are you need to grab the ID and/or object of the current item…
Categories:
Bricks Query Loop – Posts Authored by Logged in User

Bricks Query Loop – Posts Authored by Logged in User

How to display only the posts authored by the currently logged-in user in a query loop.
Categories:
Pro
Posts Grouped by Month and Year in Bricks

Posts Grouped by Month and Year in Bricks

Nesting query loops with months & years in descending order, with the inner loop outputting the posts.
Categories:
Pro
Events grouped by months within years based on event date custom field value

Events grouped by months within years based on event date custom field value

How to output events grouped by years and months using three nested query loops in Bricks.
Categories:
Pro
Related Posts by Taxonomy in Bricks

Related Posts by Taxonomy in Bricks

In the previous Posts Related by Current Post's Terms in Bricks tutorial, we showed how posts related to the current single post that have the…
Categories:
How to add a layout toggle on query loops in Bricks

How to add a layout toggle on query loops in Bricks

In this tutorial, we’ll show you how to change the layout of your query loop container based on the click of an icon toggle sitting…
Categories:
How to apply a custom query var to multiple query loops in Bricks

How to apply a custom query var to multiple query loops in Bricks

In this tutorial, we'll learn how to apply a PHP filter to modify the query var of multiple query loops in a single function Introduction…
Categories: