Reputation: 23
Is there a way to add a custom control (button or toggle) to an Advanced Custom Fields group, where it can be hidden from the front-end with a toggle or a button? I am trying to add a user friendly way to allow the client to hide a block of info at their leisure.
I don't really have a code example as it's really just a general question. I do a lot of custom ACF pro layouts, and I user their docs and ACF awesome quite a bit. I have just never heard of this being used and it seems like a simple thing that clients always ask for. Just wondering what your guys' experiences are with this?
Upvotes: 2
Views: 4970
Reputation: 1084
This is extremely easy to do, just create another field thats a radio button for example or a checkbox both work. In my example i would use a field called example_toggle:
For a checkbox:
<?php if( !in_array( 'hide', get_field('example_toggle') ) ):?>
insert your acf code that you wish to display
<?php endif;?>
This will check example_toggle it checks to make sure that the "hide" box has not been checked (!in_array = not in array), if it isn't found then it will execute the block. Otherwise if selected to "hide" it will not execute the code and therefore hides the block.
For a radio button:
<?php if( get_field('example_toggle') == 'show' ):?>
insert your acf code that you wish to display
<?php endif;?>
Same explanation however, it can only have 1 option so it doesn't need to check an array it justs needs to check the value, I also use "show" in this example but "hide" can be used if you change == to !==
For true/false:
<?php if( get_field('example_toggle') ): ?>
insert your acf code that you wish to display
<?php endif;?>
Same explanation however less code required for True/False as true = 1 false = 0 it will only trigger when the value is true.
Upvotes: 1