Opens in a new tab
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.

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

5 comments

Leave your comment

 

Related Tutorials..

Pro
Month and Day Condition in Bricks

Month and Day Condition in Bricks

This Pro tutorial shows how we can define a custom function that takes in a month in the three-letter format (like Dec) and a day…
Categories:
Pro
Checking ‘PublishPress Future’ plugin’s Future Action enabled posts

Checking ‘PublishPress Future’ plugin’s Future Action enabled posts

PublishPress Future is a handy plugin for scheduling actions like unpublishing or deleting posts (of any post type) at a specified date and time in…
Categories:
Pro
Showing a Popup conditionally in Bricks based on Query Parameter

Showing a Popup conditionally in Bricks based on Query Parameter

Here's two ways a Bricks Popup can be shown on page load depending on URL parameter value.
Categories:
Tags:
Pro
City-based and Country-based Conditional Output in Bricks

City-based and Country-based Conditional Output in Bricks

This Pro tutorial provides the steps to render a Bricks element only if the visitor is from the specified country or city. Note: Caching may…
Categories:
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
Day of Week Condition in Bricks

Day of Week Condition in Bricks

Displaying elements conditionally based on the current day of the week.
Categories:
[Function] Current Term Has Children

[Function] Current Term Has Children

Updated on 20 Jul 2024 A user asks: I'm working on an archive template for WooCommerce products. I'm trying to hide a section if there…