10th Dec '22
/
8 comments

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

Related Tutorials..

Pro
ACF Repeater sub field value dynamic data condition in Bricks

ACF Repeater sub field value dynamic data condition in Bricks

This Pro tutorial provides the steps to output an element on single posts (can be of any post type) only if a specified ACF (Pro)…
Checking for Post Type in Bricks

Checking for Post Type in Bricks

A way to output elements conditionally based on the current post type in the loop in Bricks.
Categories:
Pro
At Least 1 Search Result Condition in Bricks

At Least 1 Search Result Condition in Bricks

Registering a custom condition to render elements depending on whether there is at least one search result.
Categories:
Pro
Conditional Related Posts in Bricks

Conditional Related Posts in Bricks

How to output a section only if there is at least 1 related post for the current post being viewed.
Categories: