50% discount on BricksLabs Pro Membership Lifetime Access. Use coupon: GOBRICKS

Conditions in Bricks Builder

CleanShot-2022-05-14-at-09.12.23@2x

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.

Leave the first comment