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

Instant access to 390+ Bricks code tutorials with BricksLabs Pro