Render an element having a specific HTML ID based on value of a custom field

Render an element having a specific HTML ID based on value of a custom field

This tutorial provides the steps to use Bricksbricks/element/render filter to conditionally output an element that has the specified HTML ID based on the value of a specific custom field of the current post when viewing a singular page.

In this example, we shall use ACF to create a field group attached to Pages that has a single True / False type of field – hide_footer_cta and set a Section whose HTML ID is footer-cta to be output only on Pages for which that field has been set to false.

Creating the custom field
Entering HTML ID for an element in Bricks builder
Populating the custom field

When Hide Footer CTA has been turned on i.e., set to Yes, the Section should not be rendered.

Here’s the code for this to be added in child theme’s functions.php or a code snippets plugin like WPCodeBox:

// Render an element with "footer-cta" HTML ID if the specified condition is true.
// Condition: The value of a custom field "hide_footer_cta" is false.
add_filter( 'bricks/element/render', function( $render, $element ) {	
	// Get the element HTML ID
	$html_id = ! empty( $element->attributes['_root']['id'] ) ? $element->attributes['_root']['id'] : false;

	if ( 'footer-cta' === $html_id ) {
		return ! get_post_meta( $element->post_id, 'hide_footer_cta', true );
	}

	return $render;
}, 10, 2 );
Get access to all 524 Bricks code tutorials with BricksLabs Pro

2 comments

  • Thanks! How to do the exact opposite? Hide an element targeted by html ID, if the hide_footer_cta is FALSE.

    Can you update this article with that? ๐Ÿ™‚

    Thank you in advance!

    • Sorry, that's not what I meant... I meant hide an element if the value is true, instead of displaying it.

Leave your comment

 

Related Tutorials..

Pro
Check If The Latest Post Was Published in The Last X Minutes

Check If The Latest Post Was Published in The Last X Minutes

In this Pro tutorial we shall define a custom function that takes in the number of minutes as an argument and returns true if there…
Read more โ†’
Categories:
Pro
Condition to Check if the Current Category Has At Least One Child

Condition to Check if the Current Category Has At Least One Child

Looking to render an element in the category archive Bricks template only if the category of the current category archive page is a parent? This…
Read more โ†’
Categories:
Conditional Output on the First Page of Archives in Bricks

Conditional Output on the First Page of Archives in Bricks

How to output an element on an archive page only if we're on the first page of the archive.
Read more โ†’
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.
Read more โ†’
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…
Read more โ†’
Categories:
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…
Read more โ†’
Categories:
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…
Read more โ†’
Categories:
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…
Read more โ†’
Categories: