19th May '23
/
8 comments

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/

Get access to all 630 Bricks code tutorials with BricksLabs Pro

8 comments

  • Maddalena Fasser

    Hello Sridhar, we could also use these dynamic data tags in the condition:

    Bricks, Free Dynamic Data {post_terms_your_taxonomy_slug:plain}

    Then set the operator to “contains” and specify the taxonomy term.

    Does it make sense? Thanks, as always, for your time and support!

    (As a side note, BrickExtras users should now find a Post Category condition that makes this even more convenient.)

    • Maddalena Fasser

      I’m not sure what happened, but it seems my comment included "Bricks, Free Dynamic Data" by mistake, as I didn’t write that part. 😅 I'll try again to write the dynamic data tags: Bricks, Free Dynamic Data {post_terms_your_taxonomy_slug:plain}

      • Maddalena Fasser

        Ok, I see, Bricks actually rendered the dynamic data tags in the comments! Here they are again (without curly braces to avoid rendering):

        post_terms_category:plain post_terms_post_tag:plain post_terms_your_taxonomy_slug:plain

  • Cheok Way Leong

    Hi, what if I wanna target all the taxonomies in "project-type" instead of just "construction". Would it be = 1 or > 0?

  • 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 a Reply to Cheok Way Leong (Cancel Reply)

 

Related Tutorials..

How to conditionally load your scripts based on an ACF field

How to conditionally load your scripts based on an ACF field

This tutorial provides the PHP codes that can be pasted in order to enqueue your scripts conditionally based on the value of an ACF field…
Categories:
Pro
Bricks Dynamic Condition – Check if today falls within 5 days before any holiday date

Bricks Dynamic Condition – Check if today falls within 5 days before any holiday date

Creating a custom condition that returns true or false depending on whether today falls within 5 days before any holiday dates selected on a Meta…
Categories:
Tags:
Pro
User Role Conditional Output in Bricks

User Role Conditional Output in Bricks

This Pro tutorial provides the steps to output elements based on their CSS class to users of the specified role in WordPress when using Bricks…
Categories:
Pro
Conditionally Rendering an Element Outside the Loop based on Taxonomy Term in Bricks

Conditionally Rendering an Element Outside the Loop based on Taxonomy Term in Bricks

A user asks: Hiding element based on taxonomy I have an element in a footer that I want to hide if a page has a…
Categories:
Pro
Conditional single post Bricks template based on internal referrer URL

Conditional single post Bricks template based on internal referrer URL

How to automatically select a single post template depending on the page from which the post is accessed.
Bricks Condition for WishList Member

Bricks Condition for WishList Member

How to render a Bricks element only if the user has access to a specified WishList Member level.
Categories:
Pro
Conditional Rendering Based On Meta Box Group’s Sub Field

Conditional Rendering Based On Meta Box Group’s Sub Field

This Pro tutorial shows how we can output an element on a single post pages only if a specified sub-field (custom field created using Meta…
Conditionally Hide a Section on a Specific Taxonomy’s Term Archives in Bricks

Conditionally Hide a Section on a Specific Taxonomy’s Term Archives in Bricks

A user asked: I have a CTA in the footer of my site, which I have custom fields on various templates, etc to populate. I…
Categories: