Conditionally Outputting Elements Only for Posts That Have a Specific Taxonomy Term

A user asked:

I have a loop with some elements shown in a card.
Some items have the taxonomy “Bestseller”.
In the card I have a text with “Bestseller” that should be shown if taxonomy = “Bestseller”.
How can I do that ?

We can use the core WordPress has_term() function for checking if the current post has a specific term of a given taxonomy.

In this example, we shall work with a Bricks query loop in which the query type is set to Projects custom post type. This CPT has a corresponding Project Type taxonomy whose sample terms are Construction, Parks and Libraries.

Structure:

Now the objective is to display the last heading element only for the project items that are of Construction type.

For this we apply a Dynamic data condition on the Heading element like this:

{echo:has_term(construction,project-type)}

We are passing the term slug (name or ID can also be given) as the first argument for the has_term() function. The second argument is the taxonomy name.

Note: There should NOT be a comma between the two arguments.

Reference

https://developer.wordpress.org/reference/functions/has_term/

Instant access to 390+ Bricks code tutorials with BricksLabs Pro

1 comment

  • You say there should NOT be a comma between the two arguments, but your example does have a comma between them? Please clarify this.

Leave your comment