Conditional - hide if no data

Hi,

I know conditional is still in roadmap, but I would like to know if there is any way to hide content if there is no data in a custom field?

Thank you

Hi,

What is the type of the field?

Are you using a plugin to create the field? If so, which one?

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

Thank you @Sridhar and @Matiasko

I’ve got it working in an ACF repeater with query loop, just 2 more questions:

  1. What if I want to use it in a ACF text field? (no repeater) I’ve tried removing the “in_array” statment in the code
  2. How can I do if I have more than one field to hide/show in the same page?

Thank you again

Exactly the same, in fact, the code I gave you uses a simple ACF Text field. The in_array() part checks if in element’s array of classes there is one class we’re looking for. So if you want to hide this text field based on its class then you should just change class name. If you want to target it by ID then you need to check for this ID in the if statement. There is an example of that in docs.

It depends.

If they are wrapped in some DIV and you want to hide all of them if some condition is met, then I would simply not render the whole DIV. Which again can be done checking the class or ID.

If you want to treat them separately and (for example) hide each of them when they are empty, then you need to add a separate if in the function. Or you can simplify it with case.

To give an example of the second case:

<?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') );
  }

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

  return $render;
}, 10, 2 );

The code for multiple fields can possibly be simplified for the performance, but it depends on the context.

Thank you again @Matiasko

Could you please post the code for ACF repeater? I have the same task to solve right now!?

I guess I replied in your post, so just mentioning this here for future readers.

1 Like

where do we add this code?

Is there any easier way today?

Thanks!

Yes, it’s been a while so currently you can set it up with element conditions.

If you want to check if certain field is empty then it should work like in this answer.

1 Like