2nd Aug '24
/
0 comments

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

Related Tutorials..

Weekday Condition in Bricks

Weekday Condition in Bricks

As of Bricks 1.7, the built-in "Weekday" condition does not work correctly. This is most likely due to it not taking the site's timezone (set…
Categories:
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:
Conditional Output based on Manual Excerpt in Bricks

Conditional Output based on Manual Excerpt in Bricks

How to display a post’s excerpt only if it has been manually set.
Categories:
Pro
Conditional CSS in Bricks based on Post Meta

Conditional CSS in Bricks based on Post Meta

In the Bricks Facebook group a user asked: Hi, how do you normally control margin, padding and font-size using dynamic data? I'm looking for the…
Checking if Repeater/Cloneable Meta Fields Are Empty in Bricks

Checking if Repeater/Cloneable Meta Fields Are Empty in Bricks

Creating a condition to ensure that a section only gets output if at least one row of data is filled in.
Categories:
Pro
Conditionally Rendering an Element Outside the Loop based on Taxonomy Term in Bricks

Conditionally Rendering an Element Outside the Loop based on Taxonomy Term in Bricks

A user asks: Hiding element based on taxonomy I have an element in a footer that I want to hide if a page has a…
Categories: