Conditional Single CPT Templates based on Post Hierarchy in Bricks

In the past we covered Conditional Bricks Templates Based on Product Category Hierarchy Levels in Bricks.

This Pro tutorial provides the steps to programmatically apply a Bricks template created for Level 1 items (posts of a Custom Post Type) to CPT items that are at Level 1 (refer to the sample screenshot below), Level 2 template to all single CPT items that are at Level 2, Level 3 template to all single CPT items that are at Level 3 and so on...

Step 1

If you have not already, create a template that applies to all single posts of your CPT. Ensure that the template condition is set to Post type → Services (or whatever is your post type). This template will be like a fallback and gets used/applied for all the singular CPT pages that do not meet the criterion for the levels we are going to check for in the next step.

Next, create different templates for Level 1, Level 2, Level 3 and so on - as many levels as you need. Do not set any template conditions for these.

When done, it should appear something like this:

Note the IDs of Level 1, 2 and 3 etc. templates. We need these in the next step.

Step 2

Let's define a custom function to check if the current post's parent-child hierarchy level is the given level (number starting with 1) and use it with the bricks/active_templates filter.

Add the following in child theme‘s functions.php 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
Current Post Has Parent – Conditional Single Template

Current Post Has Parent – Conditional Single Template

How to apply one of two Bricks templates to posts depending on whether that post has a parent or not.
Pro
Dynamic Post-specific Templates in Bricks using Meta Box/ACF Select Field

Dynamic Post-specific Templates in Bricks using Meta Box/ACF Select Field

Update on 23 Aug 2023: Added steps for ACF. Bricks builder v1.8.4 introduced an pretty useful filter called bricks/active_templates that flew under the radar. This…
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.
Pro
Setting a Bricks template for Parent pages

Setting a Bricks template for Parent pages

This Pro tutorial shows how bricks/active_templates filter can be used to programmatically assign a Bricks template to all singular pages (of any post type) that…