3rd Sep '24
/
3 comments

aria-current=”true” for Current Categories on single Posts in Bricks

Updated on 11 Sep 2024

A user asks:

I have a single post template and display all categories in it with the bricks loop.

Now I would like to color all categories that are assigned to the post red.

In the category template this is easy because the categories have aria-current=“page”, but how do I do this in a single post template?

We can

  • write a custom function that returns 'true' or 'false' string depending on whether a given category is assigned to the current post
  • add an aria-current data attribute with the value set to our function for either the query loop-enabled list item or the category link text element inside in the Terms query loop
  • add custom CSS targeting [aria-current="true"] to uniquely make the post's category/categories stand out

This Pro tutorial shows how.

After implementing the tutorial, for a post that has "Business" and "People" categories:

Update 1: Added a more versatile function that works with not just the standard categories, but any taxonomy including custom taxonomies.

Step 1

Edit your single post template with Bricks.

Set up a Terms query loop on a Block.

Select "Categories (Post)" for the Taxonomy.

Add a Basic Text element (or a Text Link if you prefer that) inside the Block.

Set its text to be the term name and link to be the term URL.

Step 2

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
Checking if the current term in a Bricks Terms query loop of the given taxonomy has children

Checking if the current term in a Bricks Terms query loop of the given taxonomy has children

There could be situations when you need a way to check if the current term in a Bricks Terms query loop has children or not.…
Categories:
Pro
How to Query WordPress Taxonomy Terms by Hierarchy Level in Bricks

How to Query WordPress Taxonomy Terms by Hierarchy Level in Bricks

How to limit the terms of a Bricks Terms query to only those at a specific level within the taxonomy hierarchy.
Categories:
Pro
Term Parent Slug Data Attribute in Bricks

Term Parent Slug Data Attribute in Bricks

How we can set a data attributes on a child taxonomy dynamically based on the parent taxonomy.
Categories:
Pro
Category Grid with ACF Images in Bricks

Category Grid with ACF Images in Bricks

Creating a grid of post categories, each category card showing an image from an ACF field, category name & description.
Categories:
Pro
Terms Query in Bricks Filtered by Post Meta

Terms Query in Bricks Filtered by Post Meta

Consider this scenario: CPT: jobTaxonomy: industryCustom field: job_visibility of type Radio with a default value of public. The other value is private. Requirement: Show the…
Pro
Adding active class to the current term/post in a Bricks query loop on Term archives/single posts

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…
Categories:
Pro
Current Post’s Terms in Bricks

Current Post’s Terms in Bricks

Updated on 15 Jan 2024 This Pro tutorial provides the steps to set up a grid of terms (like categories) that the current post is…