On a blog single template inside an ACF repeater I need to show/hide a button, depending on whether an ACF text field is empty or not. Button should SHOW if acf text field has a value. Button should HIDE if acf text field is empty.
// Get the element CSS classes
$classes = $element->attributes['_root']['class'];
// Check if there is at least one class
$classes = ! empty( $classes ) ? $classes : true;
// Show or hide "acf repeater blog content button" if "acf button text" is or isn't empty
//btn-empty-check
if ( $classes && in_array( 'btn-empty-check', $classes ) )
{
$textvalueinfo = $element->settings['text'];
//print_r($element->settings);
if(!empty($textvalueinfo))
{
return $render;
}
}else
{
return $render;
}
Nope, seems clear to me. Try this code, but change the field name from “acf_button_text_field_name” to the one you’re using:
add_filter( ‘bricks/element/render’, function( $render, $element ) {
// Get the element CSS classes
$classes = $element->attributes['_root']['class'];
// Check if there is at least one class
$classes = ! empty( $classes ) ? $classes : true;
// Show or hide "acf repeater blog content button" if "acf button text" is or isn't empty
//btn-empty-check
if ( $classes && in_array( 'btn-empty-check', $classes ) )
{
// REPLACE THE NAME IN THE FOLLOWING LINE OF CODE
// Get the corresponding field value
$acf_btn_text = get_field('acf_button_text_field_name');
// Check if it is empty
$is_btn_text_empty = empty($acf_btn_text);
// If it is empty - don't render (return false) and if it isn't - render (return true)
if($is_btn_text_empty)
{
return false;
} else{
return true;
} else
{
return $render;
}
}, 10, 2 );
It seems like you based your code on the one provided by myself in the mentioned forum post. It is a bit simplified, just not to create additional variables. This might be confusing, so here I’ve done it in a more “plain” way. The way that render filter works is that it return “true” or “false” value for each element. What you were doing, was returning the initial value of $render without changing it. And this is “true” by default. You also don’t have to take the text value from the settings of en element, because it should work by calling the ACF field itself with get_field.