Conditional - hide if no data

I have a simple text ACF field which represent a short testimonial. It is attached to the portfolio item CPT. If the field is not empty, it is outputted in the portfolio grid (done with query loop).

It has a specific class portfolio-card--testimonial, so this is what I’m checking for before testing if portfolio_testimonial field is empty.

I’m using bricks/element/render filter as per documentation.

<?php

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 : false;

  // Show or hide portfolio testimonial if is or isn't empty
  if ( $classes && in_array( 'portfolio-card--testimonial', $classes ) ) {
    return ! empty( get_field('portfolio_testimonial') );
  }

  return $render;
}, 10, 2 );

As Sridhar mentioned, it depends on the custom fields framework used and the field type, but the implementation should follow the same logic.

2 Likes