11th Jul '22
/
2 comments

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 626 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..

Conditional Output based on Manual Excerpt in Bricks

Conditional Output based on Manual Excerpt in Bricks

How to display a post’s excerpt only if it has been manually set.
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.
Categories:
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…
Categories:
Pro
Conditionally Rendering an Element Outside the Loop based on Taxonomy Term in Bricks

Conditionally Rendering an Element Outside the Loop based on Taxonomy Term in Bricks

A user asks: Hiding element based on taxonomy I have an element in a footer that I want to hide if a page has a…
Categories:
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…
Categories:
Pro
Bricks Dynamic Condition – Check if today falls within 5 days before any holiday date

Bricks Dynamic Condition – Check if today falls within 5 days before any holiday date

Creating a custom condition that returns true or false depending on whether today falls within 5 days before any holiday dates selected on a Meta…
Categories:
Tags:
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: