Conditional single post Bricks template based on internal referrer URL

In the BricksLabs Facebook group, a user asks:

Wonder:

Is it possible to automatically select a single post template depending on the page from which the post is accessed?

Example: My post "Opening ceremony" appears on the page "News" and on the page "Events". If the user opens this post from the news page, template 1 should be selected automatically. If the user opens this post from the Events page, Template 2 should be selected automatically.

This Pro tutorial shows how bricks/active_templates filter can be used to show single posts in different Bricks templates for different pages based on the page URLs.

Step 1

Create three Bricks templates:

  • one for when the user visits any post from the /news page
  • another for when the user visits any post from the /events page
  • another default template

For the default template, make sure you set the template condition.

The other two should not have a template condition set.

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

Related Tutorials..

Conditional Rendering for Logged in/out Users by CSS Classes in Bricks

Conditional Rendering for Logged in/out Users by CSS Classes in Bricks

This tutorial shows how Bricks builder's bricks/element/render filter can be used to conditionally output elements having a class of logged-in to logged-in users only and…
Categories:
Pro
Query Variables Condition in Bricks

Query Variables Condition in Bricks

This Pro tutorial provides the steps to register a custom function that returns true or false depending on whether the current page URL has specific…
Pro
Conditional Output in Bricks based on if Content has Headings

Conditional Output in Bricks based on if Content has Headings

How to prevent the output of the table of contents if the content has no headings.
Categories:
Tags:
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…
Create A Customizable AJAX Add To Cart Button In Bricks Builder

Create A Customizable AJAX Add To Cart Button In Bricks Builder

In Bricks, you can simply create an Add To Cart button from a dynamic data / function {woo_add_to_cart}. This button supports AJAX as well if…
Pro
Conditional CSS Classes based on Bricks Query Count

Conditional CSS Classes based on Bricks Query Count

In a Bricks project I am currently working on, there are nested query loops - team members inside departments on a Page. Each team member…
Pro
Conditional Section when Post has a Featured Image in Bricks

Conditional Section when Post has a Featured Image in Bricks

This Pro tutorial provides the steps to output a Section in Bricks on single posts (of any post type) only if the post has a…
Categories:
Pro
Day of Week Condition in Bricks

Day of Week Condition in Bricks

Displaying elements conditionally based on the current day of the week.
Categories:
Is WooCommerce Cart Empty Condition in Bricks

Is WooCommerce Cart Empty Condition in Bricks

Updated on 5 Nov 2024 Looking to conditionally render an element depending on whether the user's cart is empty or not when using WooCommerce? Add…