14th May '22
/
5 comments

Conditions in Bricks Builder

Conditions in Bricks Builder

The current version of Bricks does not have a built-in Conditions feature but that does not mean we can’t set them up manually with custom code.

This tutorial provides a few practical examples of how to output any element in a Bricks site only if the specified condition is met.

The general process is:

  1. Create a Template of the type “Section” and build your content that should be conditionally shown.
  2. Add a Code element where you want to show the conditional content and use do_shortcode() function wrapped in an if condition as needed.
  3. If the condition is based on a custom function, ensure that you are wrapping the if conditional inside a function_exists() check.

One small downside to this approach is that there will be an empty div in the HTML output when the condition is not met.

Most of these were put together as this site was being built.

Note: Tested in Bricks 1.4 RC1.

Table of Contents

Different content for logged-in users and visitors

Logged-in users:

Non logged-in visitors:

Step 1

Create a Template of the type Section named say, “Logged In”.

Add your content that should be shown for users that are logged in inside a Container that’s inside a Section (Tip: Click on the little Layout icon at the top left of Container element after clicking +).

Step 2

Go to the Templates list screen, hover over the Template and note its ID by looking at the URL in the browser status bar or alternately copy its shortcode to a notepad.

In the Page/Template where you would like your content to be output if the user is logged-in, add a Code element having:

In the above ensure that 2485 is replaced with your Template ID.

Non logged-in users will not see anything on the front end.

If you want to show the content of a different Template for when the user is not logged-in:

Step 3

Create another Template of the type Section named say, “Not Logged In”.

Add your content that should be shown for users that are NOT logged in inside a Container that’s inside a Section.

Change the code to:

Replace the Template IDs as appropriate. The first shortcode runs when the user is logged in and the second when the user is not.

Check if the post (in the loop) is in a specific category

We have a “Pro” ribbon set for posts that have been categorized “Pro” in the posts grid in various views on this site.

Here’s the code behind the logic.

Step 1

In child theme’s functions.php, add

/**
 * Function to check if the current post is assigned to Pro category
 * @return string "Pro" if in the specified category
 */
function bl_is_in_pro_category() {
	return in_category( 'pro' ) ? 'Pro' : '';
}

Step 2

In our Posts Grid Section Template, we have:

in a Code element for the ribbon title.

With this in place, the ribbon will appear only for posts marked as Pro.

Different content for members and unauthorized members when using MemberPress

MemberPress is the membership plugin of our choice.

Custom function:

/**
 * Function to check if the current user has access to the membership.
 *
 * @return bool True if the user has access, false if not.
 */
function bl_user_has_membership() {
	return current_user_can( 'mepr-active', 'memberships:51,367' );
}

where 51 and 367 are the memberships to check against.

Sample code in a Code element to output the specified Template for authorized members:

<?php
if ( function_exists( 'bl_user_has_membership' ) ) {
	echo bl_user_has_membership() ? do_shortcode( '' ) : '';
}
?>

Sample code in a Code element to output the specified Template for unauthorized members:

<?php
if ( function_exists( 'bl_user_has_membership' ) ) {
	echo ! bl_user_has_membership() ? do_shortcode( '' ) : '';
}
?>

Alternately, a single Code element can be used having:

<?php
if ( function_exists( 'bl_user_has_membership' ) ) {
	echo bl_user_has_membership() ? do_shortcode( '' ) : do_shortcode( '' );
}
?>

Reference.

Render different Templates on search results pages depending on whether there is a matching result or not

Custom function:

// Function to check if there is at least 1 search result.
function bl_has_search_results() {
	global $wp_query;
	return $wp_query->found_posts !== 0;
}

Sample usage:

A detailed tutorial on this will follow.

Conditional Section when Post has a Featured Image in Bricks

View the tutorial here.

That’s it!

Want access to more in-depth step-by-step Bricks’ tutorials? You might want to become a Pro member.

Get access to all 610 Bricks code tutorials with BricksLabs Pro

5 comments

Leave a Reply to Mike (Cancel Reply)

 

Related Tutorials..

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…
How to conditionally load your scripts based on an ACF field

How to conditionally load your scripts based on an ACF field

This tutorial provides the PHP codes that can be pasted in order to enqueue your scripts conditionally based on the value of an ACF field…
Categories:
Pro
Conditional Related Posts in Bricks

Conditional Related Posts in Bricks

How to output a section only if there is at least 1 related post for the current post being viewed.
Categories:
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
Conditional Rendering of ACF Repeater Rows Based on True / False Sub Field in Bricks

Conditional Rendering of ACF Repeater Rows Based on True / False Sub Field in Bricks

This Pro tutorial provides the steps to modify a ACF Repeater query loop to only output the rows for which a True / False type…
Categories:
Pro
ACF Repeater sub field value dynamic data condition in Bricks

ACF Repeater sub field value dynamic data condition in Bricks

This Pro tutorial provides the steps to output an element on single posts (can be of any post type) only if a specified ACF (Pro)…
Pro
User Role Condition in Bricks

User Role Condition in Bricks

This Pro tutorial provides the steps to conditionally output elements depending on the currently logged-in user's role. Step 1 Create a section Template having the…
Categories:
Pro
How to Insert Element(s) Between Query Loop Posts in Bricks

How to Insert Element(s) Between Query Loop Posts in Bricks

Update on 16 Aug 2023: Follow this tutorial instead. This Pro tutorial shows how we can insert a Div (or any custom HTML) after nth…