-
pnwcheryl
Hi,
Now that the remove container condition is gone, I have some containers I want to remove based on ACF fields.
I found a post here:
https://generate.support/topic/generateblock-remove-container-condition/
which is based on checkboxes. I could do that: set up a checkbox that, if it’s not enabled, would remove the container. But is there a way to modify the code in that post so that if, for instance, a text field has no content the container with a particular class does not show?
Thanks -
Alvind
Hi there,
The code should work with the Text field as well. Simply replace the field name in the code with your text field name.
-
pnwcheryl
Hi,
Sorry it took so long for me to get back to this one.
Both codes work…in a way. The issue that I’m having is that if I use the code on the page that I linked to, either with a checkbox or with text, the container is visible when the checkbox is unchecked or the text field is not filled in.I want to show it if the checkbox is checked or the text field is filled in, and hide the container when it’s not.
I’ve tried revising the code a bit without success. If I change the “false” to “true” it messes up my entire layout.
How can I revise the code on that page so it hides the container when the box is unchecked or the text is not filled?
-
Try this:
add_filter( 'render_block', function( $block_content, $block ) { if ( ! is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'custom-condition-container' ) !== false ) { $checkbox = get_field( 'checkbox_field_name' ); $text_field = get_field( 'text_field_name' ); // If checkbox is checked or text field is filled, show the block if ( $checkbox || $text_field ) { return $block_content; } // If checkbox is not checked AND text is empty, hide the block return ''; } // Return original content if conditions aren't met return $block_content; }, 10, 2 );
-
pnwcheryl
Hi, thanks.
That sort of works. What I was going for were separate issues — one where I could check an ACF checkbox and then the content inside the container would show.
And a different area where I could have a container show based on if text was in a field or not.
To test out the code you provided just now, instead of putting the desired content inside the container, I added it as the preset text in the ACF text field and then put that in a text block inside the container, with the checkbox linked to whether the container showed or not to test it. I also put some other default content inside the container.
What happens is this: If the text field is not filled in, but I check the checkbox, the container does not show. If anything, including the prefilled information, is in the text field but I do not check the checkbox the container does not show.
So it works if I have default information in the text field and then use the checkbox. It would be simpler just to have a text block inside the container with the container showing if I check the checkbox — but it was doing the opposite before.
Thanks
-
So you want to 2 different classes for the containers?
One class hide when the checkbox is not checked, and the other class hide when the text field is not filled?
-
pnwcheryl
Yes. My original question was how to modify the code to use a text field instead of a checkbox. But then I found where I wanted to use a checkbox to show or hide a particular container.
However, the code did the opposite: it was showing the contents of the container when I unchecked the checkbox and was hiding it if I checked the box.
Thanks -
Try this:
add_filter( 'render_block', function( $block_content, $block ) { if ( ! is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'custom-condition-container' ) !== false ) { $checkbox = get_field( 'checkbox_field_name' ); $text_field = get_field( 'text_field_name' ); // If checkbox is checked or text field is filled, hide the block if ( $checkbox || $text_field ) { return ''; } // If checkbox is not checked AND text is empty, show the block return $block_content; } // Return original content if conditions aren't met return $block_content; }, 10, 2 );
-
pnwcheryl
Hi, thanks.
Again, for some reason, if I have the checkbox empty it does NOT show the container. Maybe I should just make “do not show” checkboxes and have them checked by default, but that seems backwards. But I can try that if necessary. My goal was to have a container with some content. Then be able to check a box if I want to show that container and hide it if the box is not checked.And have a separate code for showing a container based on whether some text was in a particular field.
-
The 2 codes I provided were opposite, I’m not sure why they result in the same output on your site, try removing the
$text_field
part from the code so the condition isolated to the checkbox.And have a separate code for showing a container based on whether some text was in a particular field.
add_filter( 'render_block', function( $block_content, $block ) { if ( ! is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'hide-container-conditioinally' ) !== false ) { $text_field = get_field( 'text_field_name' ); // If the text field is filled, show the block if ( $text_field ) { return $block_content; } // If text field is empty, hide the block return ''; } // Return original content if conditions aren't met return $block_content; }, 10, 2 );
-
pnwcheryl
That’s weird. OK.
Here’s my code; I removed the text field:
add_filter( 'render_block', function( $block_content, $block ) { if ( ! is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'editorial-use-container' ) !== false ) { $checkbox = get_field( 'is_editorial_use_only' ); // If checkbox is checked or text field is filled, hide the block if ( $checkbox ) { return ''; } // If checkbox is not checked AND text is empty, show the block return $block_content; } // Return original content if conditions aren't met return $block_content; }, 10, 2 );
My checkbox field name (as an example, if this works, I’ll do it with other checkboxes) is is_editorial_use_only
In the Additional CSS Classes for the container, I have editorial-use-container, and I have some text inside that container that I want to conditionally show if the checkbox is checked.
What is still happening is that it shows up when the checkbox is NOT checked.
Right now, this is on a staging site and not something I can link to.
-
The code is correct, I tested it on my site, and it should work.
Try disabling all your plugins except GP Premium/GB/ACF and the snippet plugin storing the PHP code.
-
pnwcheryl
Well, thanks. I did some of that and was getting the same result. I ended up using this:
add_filter('render_block', function($block_content, $block) { // Check if we're not in admin and block has className attribute if (!is_admin() && !empty($block['attrs']['className'])) { // Check for first condition/element if (strpos($block['attrs']['className'], 'all-rights-reserved-container') !== false) { $checkbox1 = get_field('not_all_rights_reserved'); if ($checkbox1) { return ''; // Hide this element if checkbox1 is true } } // Check for second condition/element if (strpos($block['attrs']['className'], 'royalty-free-container') !== false) { $checkbox2 = get_field('not_royalty_free'); if ($checkbox2) { return ''; // Hide this element if checkbox2 is true } } // Check for third condition/element if (strpos($block['attrs']['className'], 'editorial-use-container') !== false) { $checkbox3 = get_field('not_editorial_use_only'); if ($checkbox3) { return ''; // Hide this element if checkbox3 is true } } } // Return original content if no conditions are met return $block_content; }, 10, 2);
and then setting all of my checkboxes to “true” as the default value so the checkboxes are checked and the containers do not show by default, and then unchecking the box when I want a container to show.
That works, I just need to remember it’s backward from how I’d normally think about doing it:) -
That’s odd, the code should’ve done the opposite.
But anyway, as long as it works 🙂
- You must be logged in to reply to this topic.