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

Related Tutorials..

Displaying SureCart Featured Products & Products Collections with a Query Loop

Displaying SureCart Featured Products & Products Collections with a Query Loop

We walk through how to use a query loop to display SureCart products, featured products & collections.
Categories:
Pro
Posts Grouped by Authors in Bricks

Posts Grouped by Authors in Bricks

This Pro tutorial for Bricks users provides the steps to loop through users and inside that, through posts whilst setting the inner query's author parameter…
Pro
Nested Meta Box Query Loop Inside a CPT Query Loop in Bricks

Nested Meta Box Query Loop Inside a CPT Query Loop in Bricks

This Pro tutorial provides the steps for setting up a cloneable Meta Box group query inside a CPT query in Bricks. Scenario: CPT: course Meta…
Categories: