# Conditional - hide if no data

**URL:** <https://forum.bricksbuilder.io/t/conditional-hide-if-no-data/4278>\
**Category:** How To\
**Created:** [July 16, 2022, 10:40pm UTC](https://forum.bricksbuilder.io/t/conditional-hide-if-no-data/4278 "2022-07-16T22:40:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yosnap](https://forum.bricksbuilder.io/user_avatar/forum.bricksbuilder.io/yosnap/32/43_2.png) [@yosnap](https://forum.bricksbuilder.io/u/yosnap)\
**Post date:** [July 16, 2022, 10:40pm UTC](https://forum.bricksbuilder.io/t/conditional-hide-if-no-data/4278/1 "2022-07-16T22:40:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sridhar](https://forum.bricksbuilder.io/user_avatar/forum.bricksbuilder.io/sridhar/32/22855_2.png) [@Sridhar](https://forum.bricksbuilder.io/u/Sridhar)\
**Post date:** [July 17, 2022, 4:15am UTC](https://forum.bricksbuilder.io/t/conditional-hide-if-no-data/4278/2 "2022-07-17T04:15:08Z")

</div>

Hi,

What is the type of the field?

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

---

<div class="post-metadata">

**Author:** ![Matiasko](https://forum.bricksbuilder.io/letter_avatar_proxy/v4/letter/m/f08c70/32.png) [@Matiasko](https://forum.bricksbuilder.io/u/Matiasko)\
**Post date:** [July 17, 2022, 8:44am UTC](https://forum.bricksbuilder.io/t/conditional-hide-if-no-data/4278/3 "2022-07-17T08:44:37Z")

</div>

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](https://academy.bricksbuilder.io/article/filter-bricks-element-render/) filter as per documentation.

```auto
<?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.

---

<div class="post-metadata">

**Author:** ![yosnap](https://forum.bricksbuilder.io/user_avatar/forum.bricksbuilder.io/yosnap/32/43_2.png) [@yosnap](https://forum.bricksbuilder.io/u/yosnap)\
**Post date:** [July 22, 2022, 10:13am UTC](https://forum.bricksbuilder.io/t/conditional-hide-if-no-data/4278/4 "2022-07-22T10:13:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Matiasko](https://forum.bricksbuilder.io/letter_avatar_proxy/v4/letter/m/f08c70/32.png) [@Matiasko](https://forum.bricksbuilder.io/u/Matiasko)\
**Post date:** [July 22, 2022, 12:49pm UTC](https://forum.bricksbuilder.io/t/conditional-hide-if-no-data/4278/5 "2022-07-22T12:49:13Z")

</div>

> [@yosnap](#):
>
> 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

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.

> [@yosnap](#):
>
> How can I do if I have more than one field to hide/show in the same page?

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:

```auto
<?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.

---

<div class="post-metadata">

**Author:** ![yosnap](https://forum.bricksbuilder.io/user_avatar/forum.bricksbuilder.io/yosnap/32/43_2.png) [@yosnap](https://forum.bricksbuilder.io/u/yosnap)\
**Post date:** [July 24, 2022, 4:11pm UTC](https://forum.bricksbuilder.io/t/conditional-hide-if-no-data/4278/6 "2022-07-24T16:11:56Z")

</div>

Thank you again @Matiasko

---

<div class="post-metadata">

**Author:** ![beziehungsweise](https://forum.bricksbuilder.io/letter_avatar_proxy/v4/letter/b/e19adc/32.png) [@beziehungsweise](https://forum.bricksbuilder.io/u/beziehungsweise)\
**Post date:** [July 24, 2022, 8:30pm UTC](https://forum.bricksbuilder.io/t/conditional-hide-if-no-data/4278/7 "2022-07-24T20:30:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Matiasko](https://forum.bricksbuilder.io/letter_avatar_proxy/v4/letter/m/f08c70/32.png) [@Matiasko](https://forum.bricksbuilder.io/u/Matiasko)\
**Post date:** [July 25, 2022, 1:48pm UTC](https://forum.bricksbuilder.io/t/conditional-hide-if-no-data/4278/8 "2022-07-25T13:48:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![prj](https://forum.bricksbuilder.io/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@prj](https://forum.bricksbuilder.io/u/prj)\
**Post date:** [May 17, 2023, 1:33pm UTC](https://forum.bricksbuilder.io/t/conditional-hide-if-no-data/4278/9 "2023-05-17T13:33:46Z")

</div>

where do we add this code?

Is there any easier way today?

Thanks!

---

<div class="post-metadata">

**Author:** ![Matiasko](https://forum.bricksbuilder.io/letter_avatar_proxy/v4/letter/m/f08c70/32.png) [@Matiasko](https://forum.bricksbuilder.io/u/Matiasko)\
**Post date:** [May 17, 2023, 6:13pm UTC](https://forum.bricksbuilder.io/t/conditional-hide-if-no-data/4278/10 "2023-05-17T18:13:05Z")

</div>

Yes, it’s been a while so currently you can set it up with [element conditions](https://academy.bricksbuilder.io/article/element-conditions/).

If you want to check if certain field is empty then it should work like [in this answer](https://forum.bricksbuilder.io/t/conditional-hide-div-if-custom-field-is-empty/6618/3).
