Archived topic
generate_block_element_display based on acf text field value
3 replies · Started by akal on March 15, 2022
Hi,
like Dan = here https://generatepress.com/forums/topic/generate_hook_element_display/
I'm trying to display a GP element based on ACF specific text field value (not checkbox like Dan, just simple ACF text field)
- I've created an Element type: Bloc - Hook | generate_before_main_content (ID_2415)
- I've created an ACF text field: "body_classname" with rule: display on post type = page
- On specific pages that should display this Element, I've put "subcp" value in the ACF text field
Here is my code in functions.php:
//add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
add_filter( 'generate_block_element_display', function( $display, $element_id ) {
global $post;
if ( 2415 === $element_id && get_field('body_classname') == "subcp" ) {
$display = true;
}
return $display;
}, 10, 2 );
But it's not working, the Element doesn't show... Could you please tell me what is wrong ?
Best regards
akal
Hi there,
If you remove the snippet and simply set the Block Element display rules - does this appear where you expect it to?
This will confirm whether the element is doing what you need it to.
If it does then leave it as is and now use the following snippet to REMOVE the element if it does not contain the CF value you expect:
add_filter( 'generate_element_display', function( $display, $element_id ) {
global $post;
if ( 2415 === $element_id && get_field('body_classname') !== "subcp" ) {
$display = false;
}
return $display;
}, 10, 2 );
NOTE: The filter i used is a more recent addition - it applies to ANY element:
https://docs.generatepress.com/article/generate_element_display/
Hi David,
thanks to your solution, this is now working as expected
Many thanks for your fast & efficient support <3
Best regards
akal
Glad to hear that!