12th Jan '24
/
2 comments

Conditional Posts page template in Bricks

Updated on 15 Apr 2024

In the Bricks Facebook group a user asks:

Archive Template Layout based on ACF ValueHi guys, do you have an idea how set up the following scenario:I want to use Bricks in combination with ACF Pro.Based on an ACF value which you can change on an Options Page created with ACF Pro, I want to change the used Archive Template for my blog posts.E.g. Today, I choose Option 1 and all blog archives are black. Tomorrow I choose Option 2 and everything is white…I just can't figure out how to set a bricks template condition for that scenario that it will take the ACF field into consideration.What I want to prevent is to have all different layouts in one Bricks Template and only show the respective one (based on the setting) and hide all the others?

This Pro tutorial for Bricks users provides the steps to apply a template chosen in ACF Pro's options page to the blog posts index page, also known as the Posts page using bricks/active_templates filter.

example.com/blog:

With

for example,

While this tutorial is written for ACF Pro, it should also work when using other custom fields plugins like Meta Box with minor changes. If you need any help or guidance with this, please leave a comment.

Step 1

Create a Page titled "Blog" or "News".

Set it as the Post page at Settings → Reading.

Step 2

At Bricks → Templates, create two templates of the type "Archive".

Edit each with Bricks and add your desired elements like a heading text and Posts element or a query loop.

Do not apply template conditions.

Step 3

Install and activate ACF Pro.

Go to ACF → Options Pages and create a new Options page.

Step 4

Create a corresponding field group.

Field type: Button Group

Field label: Blog Posts Index Layout

Field Name: blog_posts_index_layout

Choices:

273 : Layout 1274 : Layout 2

Replace 273 with the ID of your Layout 1 template and 274 with that of your Layout 2 template.

Step 5

Click on "Site Settings" in the admin menu, select your desired layout and click Save/Update.

Step 6

Add the following in child theme's functions.php (omit the beginning PHP tag if going this route) 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 624 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

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
Programmatically populating ACF field values in WordPress

Programmatically populating ACF field values in WordPress

An example of how to set the values of a Select-type field with the names and labels of all public post types.
Categories:
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.
How to Exclude Elements from Bricks Editor

How to Exclude Elements from Bricks Editor

Bricks has a handy bricks/builder/elements filter using which the available elements that show up after clicking + (or ⌃/⌘ + ⇧ + E) in the…
Categories:
Tags: