Posts Related by Taxonomy in Bricks

It is a common requirement to show posts (posts/CPT items) that belong to the same taxonomy (category/tag/custom taxonomy) as the current single post.

This Pro tutorial shows how this can be done in Bricks taking the example of testimonial Custom Post Type and testimonial_type as the taxonomy.

We are going to

  • exclude the current single testimonial post from being output in the related testimonials list.
  • add "s" at the end of "Related Testimonial" heading when there is more than one related testimonial.
  • ensure that the Section gets output only if there is at least one related testimonial.

to limit the posts to those that belong to the same taxonomy term as the current single testimonial post.

Step 1

In your single post/CPT Bricks template, add a Section.

Inside the Section's Container, add a Heading that reads something like "Related Testimonial".

Add a Posts element or set up a Query Loop.

Set your post type, the number of posts to be pulled and tick "Exclude current post".

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

Related Tutorials..

Pro
ACF Relationship Select Filter in Bricks

ACF Relationship Select Filter in Bricks

In the BricksLabs Facebook group, a user asks: I'm learning about ACF relationships and attempting to output a list of posts on any given page,…
Categories:
Pro
Previous Two Posts in Bricks Query Loop

Previous Two Posts in Bricks Query Loop

This Pro tutorial shows how we can modify the parameters of a Bricks query to limit the posts to the previous two whilst cycling to…
Pro
Checking if the current Page/Post has Children i.e. is a Parent in Bricks

Checking if the current Page/Post has Children i.e. is a Parent in Bricks

Shows how we can check whether the current Page or Post (of any hierarchical post type) is a parent
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 Outputting Based on Query Count in Bricks

Conditionally Outputting Based on Query Count in Bricks

Update on 22 Sep 2023: There's a much simpler built-in method now. See this post. In the Bricks Facebook group a user asks: Is there…
Categories:
Show Product Categories Count Using Bricks Filter Hook

Show Product Categories Count Using Bricks Filter Hook

Unable to find terms count from the Dynamic Data provided by Bricks theme? It's okay, this simple tutorial shows you how to properly get the…
Categories:
Tags:
Pro
Filtering a Bricks Terms query on Term archive pages

Filtering a Bricks Terms query on Term archive pages

On product category archives, how to show product type names but only those that the current product category has.